Skip to content

Commit 5e550c7

Browse files
committed
add exception handling; add description of chart types; enhance comments
1 parent 38cd4fb commit 5e550c7

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

Diff for: 3.Excel Report/2.add-charts.py

+48-8
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,80 @@
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+
19
from openpyxl import load_workbook
210
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')
316

417
# 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)
737

838
# Active rows and columns
939
min_column = wb.active.min_column
1040
max_column = wb.active.max_column
1141
min_row = wb.active.min_row
1242
max_row = wb.active.max_row
43+
# print(' [active cells=({},{}):({},{})] '.format(min_row,min_column,max_row,max_column))
1344

1445
# Instantiate a barchart
1546
barchart = BarChart()
1647

1748
# Locate data and categories
49+
# data reference omits the column with the category headers
1850
data = Reference(sheet,
1951
min_col=min_column+1,
2052
max_col=max_column,
2153
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
2458
categories = Reference(sheet,
2559
min_col=min_column,
2660
max_col=min_column,
2761
min_row=min_row+1,
28-
max_row=max_row) # not including headers
62+
max_row=max_row
63+
)
2964

65+
# Make chart
3066
# Adding data and categories
3167
barchart.add_data(data, titles_from_data=True)
3268
barchart.set_categories(categories)
3369

34-
# Make chart
35-
sheet.add_chart(barchart, "B12")
3670
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
3775
barchart.style = 5 # choose the chart style
76+
sheet.add_chart(barchart, "B12")
3877

3978
# Save workbook
40-
wb.save('barchart.xlsx')
79+
wb.save(OUT_FILE)
80+
print(" --> '{}'".format(OUT_FILE))

Diff for: 3.Excel Report/barchart.xlsx

-6.68 KB
Binary file not shown.

Diff for: 3.Excel Report/pivot_table.xlsx

-5 KB
Binary file not shown.

0 commit comments

Comments
 (0)