How to delete page in Excel file using Python

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:

  1. import openpyxl #We connect library for work with Excel
  2. wb = openpyxl.load_workbook('testfile.xlsx') #Open Excel-file
  3. sheet = wb.sheetnames #Got a list of all sheets in the file and drove it into a variable
  4. print(sheet) #A list of all sheets in the file was displayed
  5. pfd = wb['1'] #We made the page we want to delete active, where [1] is the name of the page. It’s clear that you can make it a variable ;)
  6. wb.remove(pfd) #Deleting this page
  7.'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 🙂