Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get sheet names using openpyxl in Python?
In this article, we will show you how to get all the sheet names found in an excel file using Python's openpyxl library.
Assume we have taken an excel file with the name sampleTutorialsPoint.xlsx containing multiple sheets with some random data. We will return all the sheet names found in a given excel file using the sheetnames attribute.
Sample Excel File Structure
Our example file sampleTutorialsPoint.xlsx contains three sheets:
firstSheet
| Player Name | Age | Type | Country | Team | Runs | Wickets |
|---|---|---|---|---|---|---|
| Virat Kohli | 33 | Batsman | India | Royal Challengers Bangalore | 6300 | 20 |
| Bhuvaneshwar Kumar | 34 | Batsman | India | Sun Risers Hyderabad | 333 | 140 |
| Mahendra Singh Dhoni | 39 | Batsman | India | Chennai Super Kings | 4500 | 0 |
| Rashid Khan | 28 | Bowler | Afghanistan | Gujarat Titans | 500 | 130 |
SheetTwo
| David Warner | 34 | Batsman | Australia | Delhi Capitals | 5500 | 12 |
| Kieron Pollard | 35 | All rounder | West Indies | Mumbai Indians | 3000 | 67 |
| Rohit Sharma | 33 | Batsman | India | Mumbai Indians | 5456 | 20 |
3rdsheet
| Tutorials Point | Tutorials Point |
|---|
Installation
First, install the openpyxl library using pip ?
pip install openpyxl
Algorithm
Following are the steps to get sheet names from an Excel file ?
- Import the openpyxl module
- Create a variable to store the path of the input Excel file
- Load the workbook using
load_workbook()function - Access sheet names using the
sheetnamesattribute - Iterate through the list and print each sheet name
Example
The following program prints all the sheet names found in an input Excel file ?
# importing openpyxl module
import openpyxl
# input excel file path
inputExcelFile = "sampleTutorialsPoint.xlsx"
# creating or loading an excel workbook
newWorkbook = openpyxl.load_workbook(inputExcelFile)
# printing all the sheetnames in an excel file using sheetnames attribute
print('The Sheet names of the given excel file: ')
# Getting the sheetnames as a list using the sheetnames attribute
sheetNames = newWorkbook.sheetnames
# Traversing in the sheetNames list
for name in sheetNames:
print(name)
Output
The Sheet names of the given excel file: firstSheet SheetTwo 3rdsheet
Alternative Method ? Direct Access
You can also directly print the sheet names without storing them in a variable ?
import openpyxl
# Load the workbook
workbook = openpyxl.load_workbook("sampleTutorialsPoint.xlsx")
# Print sheet names directly
print("Sheet names:", workbook.sheetnames)
Sheet names: ['firstSheet', 'SheetTwo', '3rdsheet']
Conclusion
The sheetnames attribute of openpyxl's workbook object provides a simple way to get all sheet names from an Excel file. This is useful for dynamically processing multiple sheets or validating sheet structure before data operations.
