List sheets in workbook
Tags: #excel #list #sheets #workbook #data #analysis
Last update: 2023-04-12 (Created: 2023-03-29)
Description: This notebook will list the sheet's name in an Excel workbook.
References:
import openpyxl
import os
file_path
: path of the Excel workbooksheets
: list of sheets in Excel workbook
# Inputs
file_path = "Conso.xlsx"
# Outputs
sheets = []
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.")
print(sheets)
Last modified 1mo ago