[英]Python - How to prevent for loop overwriting same excel sheet
Recently I been working on my small project that is auto generating work hours spreadsheets for me. 最近,我正在从事一个小的项目,该项目可以自动为我生成工时电子表格。 It's been working well for me only but when I want to generate multiple worksheets (later on refereed as sheet) and or generate multiple spreadsheets It overwrites the old one.
它对我来说一直很好,但是当我想要生成多个工作表(后来称为工作表)或生成多个电子表格时,它将覆盖旧的工作表。
For opening, editing and generating the spreadsheet I'm using openpyxl lib. 对于打开,编辑和生成电子表格,我使用的是openpyxl lib。
I have tried moving save part and generate part outside of the loops and etc. but none of that helped me. 我尝试过移动保存部分并在循环等之外生成部分,但是这些都没有帮助我。
Question is how do I prevent for loop from overwriting the file and instead of that generating the new one ? 问题是如何防止for循环覆盖文件,而不是生成新文件?
Here's the main piece of that software down below. 这是下面该软件的主要部分。
cols = [2, 3, 4, 12]
row = 9
hrs = 0
nhrs = 0
for worker in workers:
sheet.title = ('{} {}'.format(worker.name, worker.surename))
for day, hday in itertools.zip_longest(days, holidays):
if day.weekday() <= 4 and day not in holydays:
sheet.cell(row=row, column=cols[0]).value = '08:00'
sheet.cell(row=row, column=cols[1]).value = '16:00'
sheet.cell(row=row, column=cols[2]).value = '8 h'
hrs += 8
row += 1
elif day.weekday() > 4 and day not in holidays:
sheet.cell(column=cols[0], row=row).value = ''
row += 1
elif day in holidays:
sheet.cell(column=cols[3], row=row).value = '8 h'
nhrs += 8
row += 1
sheet.cell(column=cols[2], row=40).value = str(hrs) + ' h'
sheet.cell(column=cols[3], row=40).value = str(nhrs) + ' h'
workbook.save('SPREADSHEET_{}.xlsx'.format(native_month))
print('Done')
Thanks in advance ! 提前致谢 !
Edit from comment: 编辑评论:
# native_month for the workbook name is provided by this function applied to a 1-12
def month_name_native(month):
months = ["Unknown", "Januar", "Februar", "Mart", "April", "Maj", "Jun",
"Jul", "August", "Septembar", "Oktobar", "Decembar" ]
return months[month].upper()
If I understand you correctly, you want for each worker a new spreadsheet, right? 如果我对您的理解正确,那么您想要为每个员工提供一个新的电子表格,对吗?
What you are currenctly doing is that you use the same sheet object over and over again, thus it gets overwritten. 当前执行的操作是一次又一次地使用同一个工作表对象,因此它会被覆盖。 You have to create a new sheet inside the for loop for each worker.
您必须在for循环内为每个工作人员创建一个新工作表。 This should do:
应该这样做:
cols = [2, 3, 4, 12]
row = 9
hrs = 0
nhrs = 0
workbook = Workbook() # assuming you imported openpyxl with *, else adjust
for worker in workers:
sheet = workbook.create_sheet('{} {}'.format(worker.name, worker.surename))
for day, hday in itertools.zip_longest(days, holidays):
if day.weekday() <= 4 and day not in holydays:
sheet.cell(row=row, column=cols[0]).value = '08:00'
sheet.cell(row=row, column=cols[1]).value = '16:00'
sheet.cell(row=row, column=cols[2]).value = '8 h'
hrs += 8
row += 1
elif day.weekday() > 4 and day not in holidays:
sheet.cell(column=cols[0], row=row).value = ''
row += 1
elif day in holidays:
sheet.cell(column=cols[3], row=row).value = '8 h'
nhrs += 8
row += 1
sheet.cell(column=cols[2], row=40).value = str(hrs) + ' h'
sheet.cell(column=cols[3], row=40).value = str(nhrs) + ' h'
workbook.save('SPREADSHEET_{}.xlsx'.format(native_month))
print('Done')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.