Skip to content

Commit 477a77f

Browse files
committed
Add the error checks; input month if not passed on command line
1 parent eeaa9f2 commit 477a77f

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

3.Excel Report/6.py-to-exe.py

+41-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,52 @@
1+
#!/usr/bin/which python3
2+
# 6.py-to-exe.py
3+
# loads the Excel pivot_table.xlsx 'Report' workbook created in 1.make-pivot-table.py
4+
# select the 'Report' sheet
5+
# create bar chart
6+
# create total summary of categories
7+
# annotate cells A1 and A2 with titles
8+
# output to excel from openpyxl import load_workbook
19
from openpyxl import load_workbook
210
from openpyxl.chart import BarChart, Reference
311
from openpyxl.utils import get_column_letter
412
from openpyxl.styles import Font
513
import os
614
import sys
15+
PROG = os.path.basename(sys.argv[0])
16+
IN_FILE = os.path.expanduser('pivot_table.xlsx')
717

8-
# Preparing script before we convert it to executable
9-
application_path = os.path.dirname(sys.executable)
18+
if not os.path.exists(IN_FILE):
19+
print("{} -- file '{}' not found".format(PROG, IN_FILE))
20+
exit(1)
1021

1122
# Putting together #2, #3, and #4 (input: pivot_table.xlsx + month , output: Report with barchart, formulas and format)
12-
month = input('Introduce month: ')
23+
24+
if len(sys.argv) == 1: # didn't pass month on command line
25+
MONTH = input('Report Month: ')
26+
else:
27+
MONTH = os.path.basename(sys.argv[1])
28+
OUT_FILE = os.path.expanduser(f'report_{MONTH}.xlsx')
1329

1430
# Read workbook and select sheet
15-
input_path = os.path.join(application_path, 'pivot_table.xlsx')
16-
wb = load_workbook(input_path)
17-
sheet = wb['Report']
31+
# Preparing script before we convert it to executable
32+
# application_path = os.path.dirname(sys.executable)
33+
# input_path = os.path.join(os.path.dirname(sys.executable), IN_FILE) # don't store report with app
34+
input_path = IN_FILE
35+
# if the pivot_table.xlsx file has been modified by IntelliJ's ExcelReader,
36+
# this will throw a 'KeyError' exception, so trap it and handle that
37+
# NOTE: opening the file with Apple's Numbers or Excel does not cause this error
38+
try:
39+
wb = load_workbook(IN_FILE)
40+
except KeyError:
41+
print("{} -- error opening '{}'... regenerate the file".format(PROG, IN_FILE))
42+
exit(1)
43+
44+
try:
45+
sheet = wb['Report'] # not defined if Sheet not found...throws KeyError
46+
print("{}--> '{}({})'".format(PROG, IN_FILE, sheet), end='', flush=True)
47+
except KeyError:
48+
print("{} -- error opening '{}' -- workbook or sheet not found".format(PROG, IN_FILE))
49+
exit(1)
1850

1951
# Active rows and columns
2052
min_column = wb.active.min_column
@@ -54,9 +86,9 @@
5486

5587
# Add format
5688
sheet['A1'] = 'Sales Report'
57-
sheet['A2'] = month
89+
sheet['A2'] = MONTH
5890
sheet['A1'].font = Font('Arial', bold=True, size=20)
5991
sheet['A2'].font = Font('Arial', bold=True, size=10)
6092

61-
output_path = os.path.join(application_path, f'report_{month}.xlsx')
62-
wb.save(output_path)
93+
wb.save(OUT_FILE)
94+
print(" --> '{}'".format(OUT_FILE))

0 commit comments

Comments
 (0)