|
| 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 |
1 | 9 | from openpyxl import load_workbook
|
2 | 10 | from openpyxl.chart import BarChart, Reference
|
3 | 11 | from openpyxl.utils import get_column_letter
|
4 | 12 | from openpyxl.styles import Font
|
5 | 13 | import os
|
6 | 14 | import sys
|
| 15 | +PROG = os.path.basename(sys.argv[0]) |
| 16 | +IN_FILE = os.path.expanduser('pivot_table.xlsx') |
7 | 17 |
|
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) |
10 | 21 |
|
11 | 22 | # 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') |
13 | 29 |
|
14 | 30 | # 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) |
18 | 50 |
|
19 | 51 | # Active rows and columns
|
20 | 52 | min_column = wb.active.min_column
|
|
54 | 86 |
|
55 | 87 | # Add format
|
56 | 88 | sheet['A1'] = 'Sales Report'
|
57 |
| -sheet['A2'] = month |
| 89 | +sheet['A2'] = MONTH |
58 | 90 | sheet['A1'].font = Font('Arial', bold=True, size=20)
|
59 | 91 | sheet['A2'].font = Font('Arial', bold=True, size=10)
|
60 | 92 |
|
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