Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jupyter-naas
GitHub Repository: jupyter-naas/awesome-notebooks
Path: blob/master/Excel/Excel_List_sheets_in_workbook.ipynb
2972 views
Kernel: Python 3

Excel.png

Excel - List sheets in workbook

Give Feedback | Bug report

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:

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)