I want to delete a sheet from my Excel file and i am trying this code:
import openpyxl
workbook1 = openpyxl.load_workbook(input_file_folder + input_file_name)
print(workbook1.sheetnames)
Sheet1 = workbook1['Sheet1']
workbook1.remove(Sheet1)
workbook1.save(input_file_folder + input_file_name)
writer.save()
The sheet names are printing out to be:
['Sheet1', 'Candidate Campaign 0', 'Candidate Campaign 6', 'Candidate Campaign 7', 'Candidate Campaign 8', 'Valid Campaigns']
But somehow the "Sheet1' is not getting deleted anyhow.
I even tried:
n = workbook1.sheetnames
workbook1.remove(n[1])
but this doesnt work as well.
Can anyone please pinpoint what's wrong. As this command is working with other sheets but only Sheet1
(the default one) is not getting deleted.
To start using Xlwings, there are certain basic steps which are to be done almost every time. This includes opening an Excel file, viewing the sheet available and then selecting a sheet. Sheet 1 of data. xlsx file.
del workbook1['Sheet1']
As suggested by @Charlie Clark
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With