Print just the sheetnames with Python

Question:

How can I print just the sheet names. I using a loop that gets the sheets from range 2 to 9 and prints but I would like to remove the label around it and just print the name of the sheet.

import glob
import openpyxl

path = 'C:/ExcelFolder/*.xlsx'
files = glob.glob(path)

for file in files:
wb = openpyxl.load_workbook(file)

for n in range(2, 9):
    SheetName = wb.worksheets[n]
    print(SheetName)

Output example:

<Worksheet "Monday">
<Worksheet "Tuesday">
<Worksheet "Wednesday">
<Worksheet "Thursday">
<Worksheet "Friday">
<Worksheet "Saturday">
Asked By: wisenhiemer

||

Answers:

How about: print(wb.sheetnames)

Answered By: Charlie Clark
from openpyxl.reader.excel import load_workbook
import glob

path = '*.xlsx'
files = glob.iglob(path)
for fileName in files:
    wb=load_workbook(fileName)
    w = wb.sheetnames
    for sheetname in w:
        print sheetname
Answered By: Gopal Choudhary

Two methods. The first is similar to the answer by @Gopal Choudhary

For a .xlsx, you could use glob and openpyxl.

path = 'C:path/*.xlsx'
files = glob.iglob(path)
for fileName in files:
    wb = openpyxl.load_workbook(fileName)
    w = wb.sheetnames
    for sheetname in w:
        print(sheetname)

For a .xls you could use pandas.

sheet_to_df_map = pd.read_excel('file_name', sheet_name=None)
Answered By: mcbridecaleb

If you are using pandas read_excel method.
You can retrieve the sheet names using the dictionaries keys method, as sheet names are stored in dictionaries.

xls = pd.read_excel(url, sheet_name = None)
print(xls.keys())
Answered By: CRBelhekar

Since you are using openpyxl

from openpyxl import load_workbook
ar_source = 'My_file"
workbook = load_workbook(filename=ar_source)
tabs = workbook.sheetnames
print ('Workbook: ',tabs)
for tab in tabs:
    print (tab)

My test output lists each sheet (tab at the bottom) in the entire workbook:

Workbook:  ['Review SC', 'Review AV', 'AR Language', 'ISD', 'Legend']
Review SC
Review AV
AR Language
ISD
Legend
Answered By: Ishkatan
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.