diff --git a/WriteReport.py b/WriteReport.py index cb52c59..6a542e9 100644 --- a/WriteReport.py +++ b/WriteReport.py @@ -1,6 +1,13 @@ +from openpyxl.styles import Font +from openpyxl.styles import numbers + +bold = Font(bold=True) + + def Write_Report(y, columnFormat, SSheader, sheet): results = [] +# Loops through the report xml, sends the value down to the Format Check to determine what the column should be setup as and writes the results to the spreedsheet count = 0 for child in y.report: for childs in child: @@ -14,24 +21,12 @@ def Write_Report(y, columnFormat, SSheader, sheet): sheet.append(results) results = [] -# def Detail_Write_Report(y, sheet): -# results = [] -# -# for child in y.report: -# for childs in child: -# if '$' in childs.text: -# convert = childs.text -# apply = convert[1:] -# results.append(float(apply.replace(',', ''))) -# else: -# results.append(childs.text) -# sheet.append(results) -# results = [] def Format_Check(columnFormat, SSheader, childs, count): check = columnFormat[SSheader[count]] results = [] +#Checks what the column format is and sets the value accordingly. if check == "None": return childs.text elif check == "Currency": @@ -46,3 +41,52 @@ def Format_Check(columnFormat, SSheader, childs, count): return int(childs.text) else: return childs.text + + +def Format_Report(sheet, SSheader, columnFormat): + + count = 0 + rowcount = 2 + +#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 + 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)' + rowcount += 1 + count += 1 + rowcount = 2 + +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 + count = 0 + rowcount = 2 + 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 + count += 1 + rowcount = 2 + count = 0 + rowcount = 2 + +#Makes the header columns Bold + for cell in sheet["1:1"]: + cell.font = bold + for cell in sheet2["1:1"]: + cell.font = bold + + # print("Enter a filename for the report: ") + # filename = input() + wb.save('report.xlsx') diff --git a/main.py b/main.py index 201cb42..e7565a8 100644 --- a/main.py +++ b/main.py @@ -1,11 +1,11 @@ import requests from bs4 import BeautifulSoup as bs from openpyxl import workbook -from openpyxl.styles import Font + from WriteReport import * from ClientSpecific import * -bold = Font(bold=True) + def Obtain_Ticket(URL, HOST): GetTicket = """ @@ -85,6 +85,10 @@ def Run_Report(ticket, URL, HOST, REPORT_FIELDS, wb, ws, ws2, DETAIL_REPORT_FIEL for cell in ws['A']: ExternalID.append(cell.value) ExternalID.pop(0) + # weird = len(ExternalID) + # ExternalID.pop(weird-1) + # print(ExternalID) + # exit() # Reset the header SOAPAction to FindOrderID. This is needed to get the OrderID needed for Shipping Charges headers = {'Host': HOST, 'Content-Type': 'application/soap+xml; charset=utf-8', 'Content-Length': 'length', 'SOAPAction': 'http://www.pageflex.com/XmlWebServices/2004/StorefrontAPI/20041111/FindOrderID'} @@ -135,7 +139,9 @@ def Run_Report(ticket, URL, HOST, REPORT_FIELDS, wb, ws, ws2, DETAIL_REPORT_FIEL for w in y.find_all('fval'): shippingcost.append(float(w.text)) -# Add columns to Summary Tab + + ### THIS IS WHERE THE EXTRA SUMMARY TAB SECTION WAS ### + # Add columns to Summary Tab ws.insert_cols(2) ws['B1'] = "Match" @@ -146,23 +152,21 @@ def Run_Report(ticket, URL, HOST, REPORT_FIELDS, wb, ws, ws2, DETAIL_REPORT_FIEL ws.insert_cols(7) ws['G1'] = "Adjustments" - -# Load Shipping Charges into the sheet + # Load Shipping Charges into the sheet count = 2 for x in shippingcost: ws.cell(column=6, row=count, value=x) count = count + 1 - -# Compute Production Charges and write to spreadsheet - count = 2 + # Compute Production Charges and write to spreadsheet + count = 1 for row in ws: - value = "=H"+str(count)+"-G"+str(count)+"-F"+str(count) - ws.cell(column=5, row=count, value=value) + if count != 1: + value = "=H" + str(count) + "-G" + str(count) + "-F" + str(count) + ws.cell(column=5, row=count, value=value) count += 1 - -############# Order Details tab ############# + ############# Order Details tab ############# print("Getting Item Detail") @@ -189,23 +193,16 @@ def Run_Report(ticket, URL, HOST, REPORT_FIELDS, wb, ws, ws2, DETAIL_REPORT_FIEL temp2 = temp1[1].split('') report = (temp2[0]) - y = bs(report, "lxml") - # Opens report file and writes each row sheet = ws2 Write_Report(y, detail_columnFormat, detail_SSheader, sheet) # Finalize Spreadsheet and save - - for cell in ws["1:1"]: - cell.font = bold - for cell in ws2["1:1"]: - cell.font = bold - print("Enter a filename for the report: ") - filename = input() - wb.save('report.xlsx') + Format_Report(ws, summary_SSheader, summary_columnFormat) + Format_Report(ws2, detail_SSheader, detail_columnFormat) + Save_Report(wb, ws, ws2, summary_SSheader, summary_columnFormat) def loadMenu():