Cleaned some sections up a bit to make format checking and column styling better.

This commit is contained in:
Dan Dembinski
2019-06-25 16:42:47 -04:00
parent 28fce7e10f
commit 988159af7e
2 changed files with 76 additions and 35 deletions

View File

@@ -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): def Write_Report(y, columnFormat, SSheader, sheet):
results = [] 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 count = 0
for child in y.report: for child in y.report:
for childs in child: for childs in child:
@@ -14,24 +21,12 @@ def Write_Report(y, columnFormat, SSheader, sheet):
sheet.append(results) sheet.append(results)
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): def Format_Check(columnFormat, SSheader, childs, count):
check = columnFormat[SSheader[count]] check = columnFormat[SSheader[count]]
results = [] results = []
#Checks what the column format is and sets the value accordingly.
if check == "None": if check == "None":
return childs.text return childs.text
elif check == "Currency": elif check == "Currency":
@@ -46,3 +41,52 @@ def Format_Check(columnFormat, SSheader, childs, count):
return int(childs.text) return int(childs.text)
else: else:
return childs.text 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')

39
main.py
View File

@@ -1,11 +1,11 @@
import requests import requests
from bs4 import BeautifulSoup as bs from bs4 import BeautifulSoup as bs
from openpyxl import workbook from openpyxl import workbook
from openpyxl.styles import Font
from WriteReport import * from WriteReport import *
from ClientSpecific import * from ClientSpecific import *
bold = Font(bold=True)
def Obtain_Ticket(URL, HOST): def Obtain_Ticket(URL, HOST):
GetTicket = """<?xml version="1.0" encoding="utf-8"?> GetTicket = """<?xml version="1.0" encoding="utf-8"?>
@@ -85,6 +85,10 @@ def Run_Report(ticket, URL, HOST, REPORT_FIELDS, wb, ws, ws2, DETAIL_REPORT_FIEL
for cell in ws['A']: for cell in ws['A']:
ExternalID.append(cell.value) ExternalID.append(cell.value)
ExternalID.pop(0) 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 # 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'} 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'): for w in y.find_all('fval'):
shippingcost.append(float(w.text)) 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.insert_cols(2)
ws['B1'] = "Match" 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.insert_cols(7)
ws['G1'] = "Adjustments" ws['G1'] = "Adjustments"
# Load Shipping Charges into the sheet
# Load Shipping Charges into the sheet
count = 2 count = 2
for x in shippingcost: for x in shippingcost:
ws.cell(column=6, row=count, value=x) ws.cell(column=6, row=count, value=x)
count = count + 1 count = count + 1
# Compute Production Charges and write to spreadsheet
# Compute Production Charges and write to spreadsheet count = 1
count = 2
for row in ws: for row in ws:
value = "=H"+str(count)+"-G"+str(count)+"-F"+str(count) if count != 1:
value = "=H" + str(count) + "-G" + str(count) + "-F" + str(count)
ws.cell(column=5, row=count, value=value) ws.cell(column=5, row=count, value=value)
count += 1 count += 1
############# Order Details tab #############
############# Order Details tab #############
print("Getting Item Detail") 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('<error />') temp2 = temp1[1].split('<error />')
report = (temp2[0]) report = (temp2[0])
y = bs(report, "lxml") y = bs(report, "lxml")
# Opens report file and writes each row # Opens report file and writes each row
sheet = ws2 sheet = ws2
Write_Report(y, detail_columnFormat, detail_SSheader, sheet) Write_Report(y, detail_columnFormat, detail_SSheader, sheet)
# Finalize Spreadsheet and save # Finalize Spreadsheet and save
Format_Report(ws, summary_SSheader, summary_columnFormat)
for cell in ws["1:1"]: Format_Report(ws2, detail_SSheader, detail_columnFormat)
cell.font = bold Save_Report(wb, ws, ws2, summary_SSheader, summary_columnFormat)
for cell in ws2["1:1"]:
cell.font = bold
print("Enter a filename for the report: ")
filename = input()
wb.save('report.xlsx')
def loadMenu(): def loadMenu():