Hello to all!
In the last post, we looked at the option of creating a page using the Openpyxl Python library in an Excel file. Actually – who cares – the material is available at here . But sometimes (not often) there is a need for an action that sounds like: How to delete a page in an Excel file using Python?
Imagine a situation – there is a certain excel-file in which you want to delete a sheet.
So let’s remove:
import openpyxl #We connect library for work with Excel
wb = openpyxl.load_workbook('testfile.xlsx') #Open Excel-file
sheet = wb.sheetnames #Got a list of all sheets in the file and drove it into a variable
print(sheet) #A list of all sheets in the file was displayed
pfd = wb['1'] #We made the page we want to delete active, where  is the name of the page. It’s clear that you can make it a variable ;)
wb.remove(pfd) #Deleting this page
wb.save('testdel.xlsx') #Saved file with changes (deleted page)
And of course – feel free to ask questions .
UPD: to clean the whole sheet, you can use the method:
wb.worksheets [sht] .clear () (thanks to one of the blog readers for the addition 🙂