I am trying to write to a cell range using openpyxl as a funtion in a class (not sure if I am doing it write)
I want to write to the cells in days_of_the_month_cells the caluclated values of total_cull, total_all, total_all, total_good, total_hold, and total_joblot
in this example it will be the same values for every cell range
excel_writer.py
from datetime import date
from openpyxl import Workbook, load_workbook
days_of_the_month_cells = {
"Week 1": {
"Sun": "D6:D22",
"Mon": "E6:E22",
"Tue": "F6:F22",
"Wed": "G6:G22",
"Wed": "H6:H22",
"Thu": "I6:I22",
"Fri": "J6:J22",
"Sat": "K6:K22",
}, "Week 2": {
"Sun": "D23:D39",
"Mon": "E23:E39",
"Tue": "F23:F39",
"Wed": "G23:G39",
"Wed": "H23:H39",
"Thu": "I23:I39",
"Fri": "J23:J39",
"Sat": "K23:K39",
}, "Week 3": {
"Sun": "D40:D56",
"Mon": "E40:E56",
"Tue": "F40:F56",
"Wed": "G40:G56",
"Wed": "H40:H56",
"Thu": "I40:I56",
"Fri": "J40:J56",
"Sat": "K40:K56",
}, "Week 4": {
"Sun": "D57:D73",
"Mon": "E57:E73",
"Tue": "F57:F73",
"Wed": "G57:G73",
"Wed": "H57:H73",
"Thu": "I57:I73",
"Fri": "J57:J73",
"Sat": "K57:K73",
}
}
class Write_To_Excel:
def __init__(self, cull, all, broke, good, hold, joblot):
self.cull = cull
self.all = all
self.broke = broke
self.good = good
self.hold = hold
self.joblot = joblot
def write(calculated_cull):
wb = load_workbook('XLSXs/input/Finished Goods Daily Report 2022.xlsx')
ws = wb.active
# ???
wb.save('XLSXs/output/Finished Goods Daily Report 2022.xlsx')
main.py
from excel_writer import Write_To_Excel
total_cull = ending_inventory.sum()
total_all = Total_Riper("all.pdf").calculate()
total_broke = Total_Riper("broke.pdf").calculate()
total_good = Total_Riper("good.pdf").calculate()
total_hold = Total_Riper("hold.pdf").calculate()
total_joblot = Total_Riper("joblot.pdf").calculate()
Write_To_Excel(total_cull, total_all, total_good, total_good, total_hold, total_joblot).write()
Any help you can provide would be appreciated
CodePudding user response:
you can read cell ranges and write to them this way
from datetime import date
from openpyxl import Workbook, load_workbook
wb = Workbook()
ws = wb.active
# for every week
for vs in days_of_the_month_cells.values():
# for every day, range
for k, rng in vs.items():
# for row in range
for cs in ws[rng]:
# for col in row
for c in cs:
# assign the value k for all that range
c.value = k
wb.save('foo.xlsx')