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: if childs.text is not "": asdf = Format_Check(columnFormat, SSheader, childs, count) else: asdf = childs.text results.append(asdf) count = count + 1 count = 0 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": if '$' in childs.text: convert = childs.text apply = convert[1:] results.append(float(apply.replace(',', ''))) return results[0] else: return float(childs.text) elif check == "Integer": 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')