Links

List sheets in workbook

Tags: #excel #list #sheets #workbook #data #analysis
Author: Florent Ravenel​
Last update: 2023-04-12 (Created: 2023-03-29)
Description: This notebook will list the sheet's name in an Excel workbook.
References:

Input

Import libraries

import openpyxl
import os

Setup Variables

  • file_path: path of the Excel workbook
  • sheets: list of sheets in Excel workbook
# Inputs
file_path = "Conso.xlsx"
​
# Outputs
sheets = []

Model

List sheets in workbook

The load_workbook function loads the workbook into memory, and sheetnames property returns a list of sheet names in the workbook. Finally, we loop over the sheet names and print them out.
if os.path.exists(file_path):
# Load the workbook
workbook = openpyxl.load_workbook(file_path)
​
# Get the sheet names
sheet_names = workbook.sheetnames
​
# Print the sheet names
for sheet_name in sheet_names:
print(sheet_name)
sheets += [sheet_name]
else:
print(f"❌ Excel path '{file_path}' does not exist. Please update it on your variables.")

Output

Display result

print(sheets)
Last modified 1mo ago