I am trying to use openpyxl to:
- Open an Excel (2016) workbook which contains 3 worksheets (Sheet1,Sheet2,Sheet3)
- Remove a worksheet (Sheet2)
Save the workbook to a different workbook minus Sheet2
from openpyxl import load_workbook wb = load_workbook("c:/Users/me/book1.xlsx") ws = wb.get_sheet_by_name('Sheet2') wb.remove_sheet(ws) wb.save("c:/Users/me/book2.xlsx")
The wb.save
will generate an IndexError: list index out of range
error and produce a corrupted book2.xlsx file which Excel cannot open.
del wb['Sheet2']
is an easier way to work. You'll need to supply a file. – Charlie Clark