简体   繁体   English

Python-如何防止for循环覆盖相同的Excel工作表

[英]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.

相关问题 Python Excelwriter 覆盖 excel 表 - Python Excelwriter overwriting excel sheet 如何使用python2.7.14更新具有不同数据框的同一Excel工作表而不会覆盖excel文件中存在的先前数据? - How to update the same excel sheet with different dataframe without overwriting the previous data present in excel file using python2.7.14? python:将数据框更新到现有的excel工作表,而不会覆盖同一工作表和其他工作表上的内容 - python: update dataframe to existing excel sheet without overwriting contents on the same sheet and other sheets Python:覆盖 Excel 文件中的现有工作表 - Python: overwriting an existing sheet in Excel file 防止Python For Loop覆盖字典 - Prevent Python For Loop from overwriting dictionary 如何遍历 excel 工作表并使用 python 在每张工作表上执行相同的任务 - How to loop through excel sheets and perform same task on each sheet using python 如何阻止 Excel 工作表被覆盖,我希望它在一个工作表中 - How to stop excel sheet from overwriting and I want it in a single sheet Python 防止相同循环 - Python Prevent Same Loop 在python循环中编辑Excel工作表 - Edit Excel sheet in a python loop Python Pandas:Output 到 ZBF57C906FA7D2BB66D07372E41585Dls9 覆盖选定的工作表。 - Python Pandas: Output to excel “.xls” spreadsheet with just overwriting the selected sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM