Python Read Excel xlsx File Sheet Names using OpenPyXL
In this Python openpyxl tutorial we learn how to read all sheet names of an Excel xlsx file using the openpyxl package.
How to install the openpyxl package
To install the openpyxl package using the command below.
pip install openpyxl
How to read Excel xlsx file sheet names
For example, we have an Excel file named test.xlsx with 4 sheets as below screenshot.
Below is the step by step to implement a Python program to read sheet names of the above Excel file.
Import the load_workbook function from the openpyxl package.
from openpyxl import load_workbook
Using the load_workbook function to read the Excel xlsx file into a Workbook as below.
workbook = load_workbook('test.xlsx')
Read the sheet names from the workbook as a list and print it out.
sheetnames = workbook.sheetnames
print(sheetnames)
The complete Python program as below.
read_excel_sheet_names.py
from openpyxl import load_workbook
workbook = load_workbook('test.xlsx')
sheetnames = workbook.sheetnames
print(sheetnames)
Execute the above Python program above we will get the output as below.
['Test First Sheet', 'Test Sheet 1', 'Test Sheet 2', 'Test Sheet 3']
Happy Coding 😊