|
| 1 | +#!/usr/bin/which python3 |
| 2 | +# 2.add-charts.py |
| 3 | +# loads the Excel pivot_table.xlsx 'Report' workbook created in 1.make-pivot-table.py |
| 4 | +# select the 'Report' sheet to generate the graph |
| 5 | +# select 3 columns by Gender, Product, Total Sales |
| 6 | +# create a pivot table using Gender for y-axis showing Product and Total Sales |
| 7 | +# output to excel |
| 8 | + |
1 | 9 | from openpyxl import load_workbook
|
2 | 10 | from openpyxl.chart import BarChart, Reference
|
| 11 | +import os |
| 12 | +import sys |
| 13 | +PROG = os.path.basename(sys.argv[0]) |
| 14 | +IN_FILE = os.path.expanduser('pivot_table.xlsx') |
| 15 | +OUT_FILE = os.path.expanduser('barchart.xlsx') |
3 | 16 |
|
4 | 17 | # Read workbook and select sheet
|
5 |
| -wb = load_workbook('pivot_table.xlsx') |
6 |
| -sheet = wb['Report'] |
| 18 | +if not os.path.exists(IN_FILE): |
| 19 | + print("{} -- file '{}' not found".format(PROG, IN_FILE)) |
| 20 | + exit(1) |
| 21 | + |
| 22 | +# if the pivot_table.xlsx file has been modified by IntelliJ's ExcelReader, |
| 23 | +# this will throw a 'KeyError' exception, so trap it and handle that |
| 24 | +# NOTE: opening the file with Apple's Numbers or Excel does not cause this error |
| 25 | +try: |
| 26 | + wb = load_workbook(IN_FILE) |
| 27 | +except KeyError: |
| 28 | + print("{} -- error opening '{}'... regenerate the file".format(PROG, IN_FILE)) |
| 29 | + exit(1) |
| 30 | + |
| 31 | +try: |
| 32 | + sheet = wb['Report'] # not defined if Sheet not found...throws KeyError |
| 33 | + print("{}--> '{}({})'".format(PROG, IN_FILE, sheet), end='', flush=True) |
| 34 | +except KeyError: |
| 35 | + print("{} -- error opening '{}' -- workbook or sheet not found".format(PROG, IN_FILE)) |
| 36 | + exit(1) |
7 | 37 |
|
8 | 38 | # Active rows and columns
|
9 | 39 | min_column = wb.active.min_column
|
10 | 40 | max_column = wb.active.max_column
|
11 | 41 | min_row = wb.active.min_row
|
12 | 42 | max_row = wb.active.max_row
|
| 43 | +# print(' [active cells=({},{}):({},{})] '.format(min_row,min_column,max_row,max_column)) |
13 | 44 |
|
14 | 45 | # Instantiate a barchart
|
15 | 46 | barchart = BarChart()
|
16 | 47 |
|
17 | 48 | # Locate data and categories
|
| 49 | +# data reference omits the column with the category headers |
18 | 50 | data = Reference(sheet,
|
19 | 51 | min_col=min_column+1,
|
20 | 52 | max_col=max_column,
|
21 | 53 | min_row=min_row,
|
22 |
| - max_row=max_row) # including headers |
23 |
| - |
| 54 | + max_row=max_row |
| 55 | + ) |
| 56 | +# categories being displayed are in Column A (e.g.1) |
| 57 | +# don't include the headers in min_row |
24 | 58 | categories = Reference(sheet,
|
25 | 59 | min_col=min_column,
|
26 | 60 | max_col=min_column,
|
27 | 61 | min_row=min_row+1,
|
28 |
| - max_row=max_row) # not including headers |
| 62 | + max_row=max_row |
| 63 | + ) |
29 | 64 |
|
| 65 | +# Make chart |
30 | 66 | # Adding data and categories
|
31 | 67 | barchart.add_data(data, titles_from_data=True)
|
32 | 68 | barchart.set_categories(categories)
|
33 | 69 |
|
34 |
| -# Make chart |
35 |
| -sheet.add_chart(barchart, "B12") |
36 | 70 | barchart.title = 'Sales by Product line'
|
| 71 | +# chart style |
| 72 | +# plain 1=BW 2=multi-color 3=blue 4=red 5=green 6=purple 7=cyan 8=orange |
| 73 | +# outline 9=BW 10=multi-color 11=blue 12=red 13=green 14=purple 15=cyan 16=orange |
| 74 | +# no error occurs of style > 16 |
37 | 75 | barchart.style = 5 # choose the chart style
|
| 76 | +sheet.add_chart(barchart, "B12") |
38 | 77 |
|
39 | 78 | # Save workbook
|
40 |
| -wb.save('barchart.xlsx') |
| 79 | +wb.save(OUT_FILE) |
| 80 | +print(" --> '{}'".format(OUT_FILE)) |
0 commit comments