Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting Sheet from workbook - python

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.

like image 899
zsh_18 Avatar asked Oct 28 '19 04:10

zsh_18


People also ask

How do I use python Xlwings?

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.


1 Answers


del workbook1['Sheet1']

As suggested by @Charlie Clark

like image 109
zsh_18 Avatar answered Sep 24 '22 21:09

zsh_18