How to get sheet names using openpyxl in Python
In this openpyxl tutorial, we will learn how to get all the sheet names of an Excel Workbook using openpyxl in a Python Program.
We can access and print the list of the names of the worksheets of a workbook in order by using the sheetnames
property. It returns the list of all available sheets’ names in an Excel Workbook.
Program to get sheet names using openpyxl library in Python
Let’s understand with a program:
Here we are using a workbook having 3 worksheets with different names. Our aim is to get the names of these sheets through a Python Program.
Step1: First Import the openpyxl library to the program.
import openpyxl
Step2: Load/Connect the Excel Workbook to the program.
wb = openpyxl.load_workbook("//home//codespeedy//Documents//Book2.xlsx") #give the full path of the file here
Step3: Use sheetnames
property to get the names of all the sheets of the given workbook.
print(wb.sheetnames)
Here is the complete program:
import openpyxl wb = openpyxl.load_workbook("//home//codespeedy//Documents//Book2.xlsx") print(wb.sheetnames)
Output:
['FirstSheet', 'MySheet1', 'MySheet2']
Hope you have learned how to get the names of the sheets using the openpyxl in python from this article.
Happy Coding!!!
You can also read, Arithmetic Operation in an excel file using openpyxl in Python
This is the reason why we should use python.