Skip to content Skip to sidebar Skip to footer

Can't Save A Workbook After Deleting Sheets - Openpyxl

I'm manipulating a sheet, mainly deleting all the sheets but the one that I need. I will loop through all the sheets, and delete the sheets that is not useful for my task. After de

Solution 1:

For me this is working with 2.4.2 version:

importopenpyxlworkbook= openpyxl.load_workbook('test.xlsx')

for i in workbook.worksheets:
    if i.title != 'Employee Info':
        workbook.remove_sheet(i)

workbook.save('test2.xlsx')

Try it simple to see if its working. You can also use the workbook.sheetnames to see the sheetnames:

for sheet in workbook.sheetnames:
    print(sheet)

Solution 2:

wb.remove_sheet() is deprecated from the official documentation. I can't comment yet, but do you get the same error when using wb.remove()?

Solution 3:

Before saving, you can use as a workaround:

wb.active = 0

Update to 2.4.2, last change reads: workbook.py Fail gracefully when the active sheet is deleted.

Post a Comment for "Can't Save A Workbook After Deleting Sheets - Openpyxl"