【发布时间】:2018-11-21 16:55:22
【问题描述】:
最近我一直在做我的小项目,该项目会为我自动生成工作时间电子表格。它只对我来说效果很好,但是当我想生成多个工作表(后来被称为工作表)和/或生成多个电子表格时,它会覆盖旧的。
为了打开、编辑和生成电子表格,我使用的是 openpyxl lib。
我尝试移动保存部分并在循环之外生成部分等,但这些都没有帮助我。
问题是如何防止 for 循环覆盖文件而不是生成新文件?
下面是该软件的主要部分。
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')
提前致谢!
从评论编辑:
# 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()
【问题讨论】:
-
native_month是在哪里定义的?有变化吗? -
关于Minimal, Complete, and Verifiable example,您的代码不完整 - 不包含,不创建工作簿,不向工作簿添加工作表,.....
-
本机月份是从函数(方法)month_name_native 返回的字符串,它将月份作为整数并以我的母语(塞尔维亚语)返回月份名称。请在评论中进一步找到代码 sn-p:def month_name_native(month):months = ["Unknown", "Januar", "Februar", "Mart", "April", "Maj", "Jun", "Jul ", "August", "Septembar", "Oktobar", "Decembar" ] 返回月份[月].upper()
标签: python excel loops for-loop overwrite