From f58ccd9896c48e160bcdd0f45be966f0b7af89b6 Mon Sep 17 00:00:00 2001 From: Dan Dembinski Date: Thu, 27 Jun 2019 16:50:28 -0400 Subject: [PATCH] Product summary tab is working. Added total sums to bottom. Started working on coloring the total due in Summary tab --- WriteReport.py | 69 ++++++++++++++++++++++++++++++++++++++++---------- main.py | 49 ++++++++++++++++++++++++++++++----- 2 files changed, 99 insertions(+), 19 deletions(-) diff --git a/WriteReport.py b/WriteReport.py index 6a542e9..a173641 100644 --- a/WriteReport.py +++ b/WriteReport.py @@ -1,4 +1,5 @@ from openpyxl.styles import Font +from openpyxl.styles import PatternFill from openpyxl.styles import numbers bold = Font(bold=True) @@ -18,7 +19,8 @@ def Write_Report(y, columnFormat, SSheader, sheet): results.append(asdf) count = count + 1 count = 0 - sheet.append(results) + if results[0] != "": + sheet.append(results) results = [] @@ -51,41 +53,82 @@ def Format_Report(sheet, SSheader, columnFormat): #Sets any currency related fields to the correct format. Note that this is currently only checking the headers from the config file. Anything added after the headers are loaded will cause issues. Hence the or statement in the if. #I'm working on a better way to handle this count = 0 - rowcount = 2 + rowcount = 1 for column in SSheader: check = columnFormat[SSheader[count]] if check == "Currency" or sheet['H1'].value == "Balance Due": for row in sheet.rows: - sheet.cell(column=count+1, row=rowcount).number_format = '$#,##0.00_);($#,##0.00)' + if rowcount != 1: + sheet.cell(column=count+1, row=rowcount).number_format = '$#,##0.00_);($#,##0.00)' + # if rowcount < sheet.max_row: rowcount += 1 count += 1 - rowcount = 2 + rowcount = 1 + +def Save_Report(wb, sheet, sheet2, sheet3, SSheader, columnFormat): + +# adds total fields at bottom of report + summarylength = sheet.max_row + itemlength = sheet2.max_row + productlength = sheet3.max_row + + lightGreen = Font(color='006400') + darkGreen = PatternFill(bgColor="228B22", fill_type="solid") + + sheet.cell(column = 5, row=summarylength+1, value="=SUM(E2:E"+str(summarylength)+")") + sheet.cell(column = 6, row=summarylength+1, value="=SUM(F2:F"+str(summarylength)+")") + sheet.cell(column = 7, row=summarylength+1, value="=SUM(G2:G"+str(summarylength)+")") + sheet.cell(column = 8, row=summarylength+1, value="=SUM(H2:H"+str(summarylength)+")") + # thisCell = str(summarylength+1) + # sheet[thisCell].Font = lightGreen + # sheet[thisCell].PatternFill = darkGreen + + sheet2.cell(column=7, row=itemlength + 1, value="=SUM(G2:G" + str(itemlength)+")") + sheet2.cell(column=8, row=itemlength + 1, value="=SUM(H2:H" + str(itemlength)+")") + # Format_Report misses this one. Whoops + sheet2.cell(column=8, row=itemlength + 1).number_format = '$#,##0.00_);($#,##0.00)' + + + sheet3.cell(column=2, row=productlength + 1, value="=SUM(B2:B" + str(productlength)+")") + sheet3.cell(column=3, row=productlength + 1, value="=SUM(C2:C" + str(productlength)+")") + + -def Save_Report(wb, sheet, sheet2, SSheader, columnFormat): count = 0 rowcount = 2 -#Formats a few of the hardcoded currency fields in the Order Summary Tab +#Formats a few of the hardcoded currency fields count = 0 - rowcount = 2 + rowcount = 1 for column in SSheader: check = columnFormat[SSheader[count]] for row in sheet.rows: - sheet.cell(column=5, row=rowcount).number_format = '$#,##0.00_);($#,##0.00)' - sheet.cell(column=6, row=rowcount).number_format = '$#,##0.00_);($#,##0.00)' - sheet.cell(column=7, row=rowcount).number_format = '$#,##0.00_);($#,##0.00)' - rowcount += 1 + if rowcount != 1: + # print("max", sheet.max_row) + # print("count", count) + sheet.cell(column=5, row=rowcount).number_format = '$#,##0.00_);($#,##0.00)' + sheet.cell(column=6, row=rowcount).number_format = '$#,##0.00_);($#,##0.00)' + sheet.cell(column=7, row=rowcount).number_format = '$#,##0.00_);($#,##0.00)' + if rowcount < sheet.max_row: + rowcount += 1 count += 1 rowcount = 2 - count = 0 + count = 1 rowcount = 2 -#Makes the header columns Bold + for row in sheet3.rows: + if count != 1: + sheet3.cell(column=3, row=count).number_format = '$#,##0.00_);($#,##0.00)' + count += 1 + + #Makes the header rows Bold for cell in sheet["1:1"]: cell.font = bold for cell in sheet2["1:1"]: cell.font = bold + for cell in sheet3["1:1"]: + cell.font = bold # print("Enter a filename for the report: ") # filename = input() diff --git a/main.py b/main.py index e7565a8..5b5559f 100644 --- a/main.py +++ b/main.py @@ -47,6 +47,7 @@ def Run_Report(ticket, URL, HOST, REPORT_FIELDS, wb, ws, ws2, DETAIL_REPORT_FIEL ExternalID = [] id = [] shippingcost = [] + products = set() ############# Order Summary tab ############# @@ -153,9 +154,9 @@ def Run_Report(ticket, URL, HOST, REPORT_FIELDS, wb, ws, ws2, DETAIL_REPORT_FIEL ws['G1'] = "Adjustments" # Load Shipping Charges into the sheet - count = 2 + count = 1 for x in shippingcost: - ws.cell(column=6, row=count, value=x) + ws.cell(column=6, row=count+1, value=x) count = count + 1 # Compute Production Charges and write to spreadsheet @@ -164,7 +165,8 @@ def Run_Report(ticket, URL, HOST, REPORT_FIELDS, wb, ws, ws2, DETAIL_REPORT_FIEL if count != 1: value = "=H" + str(count) + "-G" + str(count) + "-F" + str(count) ws.cell(column=5, row=count, value=value) - count += 1 + if count < ws.max_row: + count += 1 ############# Order Details tab ############# @@ -196,13 +198,48 @@ def Run_Report(ticket, URL, HOST, REPORT_FIELDS, wb, ws, ws2, DETAIL_REPORT_FIEL y = bs(report, "lxml") # Opens report file and writes each row - sheet = ws2 - Write_Report(y, detail_columnFormat, detail_SSheader, sheet) + Write_Report(y, detail_columnFormat, detail_SSheader, ws2) + + +# Product Summary Tab + ws3 = wb.create_sheet("Product Summary") + +# Load in all proudct names and dedupe + count = 1 + for x in ws2.rows: + if count != 1: + value = ws2.cell(column = 6, row = count).value + products.add(value) + count += 1 + + count = 2 + for x in products: + ws3.cell(column = 1, row = count, value = x) + count += 1 + +# Add Quantity and Item Price columns. Populate both based using SUMIF off of the Item Detail Tab + ws3['A1'] = "Product Name" + ws3['B1'] = "Quantity" + ws3['C1'] = "Item Price" + count = 1 + for x in ws3: + value = "=SUMIF('Item Detail'!F:F,A"+str(count)+",'Item Detail'!G:G)" + if count != 1: + ws3.cell(column = 2, row = count, value = value) + count += 1 + + count = 1 + for x in ws3: + value = "=SUMIF('Item Detail'!F:F,A"+str(count)+",'Item Detail'!H:H)" + if count != 1: + ws3.cell(column = 3, row = count, value = value) + count += 1 + # Finalize Spreadsheet and save Format_Report(ws, summary_SSheader, summary_columnFormat) Format_Report(ws2, detail_SSheader, detail_columnFormat) - Save_Report(wb, ws, ws2, summary_SSheader, summary_columnFormat) + Save_Report(wb, ws, ws2, ws3, summary_SSheader, summary_columnFormat) def loadMenu():