getting sheet names from openpyxl
Asked Answered
R

7

85

I have a moderately large xlsx file (around 14 MB) and OpenOffice hangs trying to open it. I was trying to use openpyxl to read the content, following this tutorial. The code snippet is as follows:

 from openpyxl import load_workbook
 wb = load_workbook(filename = 'large_file.xlsx', use_iterators = True)
 ws = wb.get_sheet_by_name(name = 'big_data') 

The problem is, I don't know the sheet name, and Sheet1/Sheet2.. etc. didn't work (returned NoneType object). I could not find a documentation telling me How to get the sheet names for an xlsx files using openpyxl. Can anyone help me?

Reorganization answered 7/5, 2014 at 20:36 Comment(0)
Z
149

Use the sheetnames property:

sheetnames

Returns the list of the names of worksheets in this workbook.

Names are returned in the worksheets order.

Type: list of strings

print (wb.sheetnames)

You can also get worksheet objects from wb.worksheets:

ws = wb.worksheets[0]
Zimmermann answered 7/5, 2014 at 20:48 Comment(5)
wb.sheetnames is the preferred spelling.Admittedly
It would be so incredibly handy to have sheet name as a property in WorkSheet class.Hackle
@ÁronLőrincz Sheet name is avaialble on a WorkSheet objects as wsObj.titleMorgenthaler
This answer is out of date. get_sheet_names() is now deprecated. Use the wb.sheetnames attribute.Runion
@Runion thanks for heads up! Updated the answer accordingly.Zimmermann
S
12

As a complement to the other answers, for a particular worksheet, you can also use cf documentation in the constructor parameters:

ws.title
Streamlet answered 18/2, 2022 at 16:25 Comment(0)
T
5

python 3.x for get sheet name you must use attribute

g_sheet=wb.sheetnames

return by list

for i in g_sheet:
    print(i)

**shoose any name **

ws=wb[g_sheet[0]]

or ws=wb[any name] suppose name sheet is paster

ws=wb["paster"]
Titanic answered 3/8, 2019 at 20:8 Comment(0)
R
3

As mentioned the earlier answer you can get the list of sheet names by using the ws.sheetnames

But if you know the sheet names you can get that worksheet object by

ws.get_sheet_by_name("YOUR_SHEET_NAME")

Another way of doing this is as mentioned in earlier answer

ws['YOUR_SHEET_NAME']
Rodeo answered 7/6, 2019 at 1:59 Comment(1)
Note .get_sheet_by_name is depreciated so you should only use the second option wb[sheetname] also noting that it is wb (workbook) not ws (worksheet) so neither answer here is completely correct.Leiker
C
1

To get the names of the sheets from workbook;

List_of_sheets = wb.sheetnames
print(List_of_sheets) 
Canaletto answered 2/9, 2023 at 5:3 Comment(0)
R
0
for worksheet in workbook:
    print(worksheet.name)
Ruthenic answered 17/11, 2022 at 20:58 Comment(1)
There is no attribute name for the worksheet object in openpyxl, so worksheet.name would just return an attribute error. The correct attribute is title so the line should be print(worksheet.title)Leiker
M
0

You can look up the worksheet object by name with the workbook name index itself.

ws = wb["big_data"]
Micro answered 30/6, 2024 at 22:2 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.