Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get number of sheet excel in python

How get number of sheet in below python example?

file = self.excel_file.decode('base64')

excel_fileobj = TemporaryFile('wb+')
excel_fileobj.write(file)
excel_fileobj.seek(0)

workbook = openpyxl.load_workbook(excel_fileobj, data_only=True)

sheet_number= ???

sheet = workbook[workbook.get_sheet_names()[0]]


for row in sheet.rows:
            print(row[1].value)

sheet_number=????

Any solutin?

like image 352
Pointer Avatar asked Mar 09 '23 13:03

Pointer


1 Answers

workbook.worksheets contains a list of worksheet objects.

To get the number of worksheets:

sheet_number = len(workbook.worksheets)

Simplest way to iterate over all worksheets:

for ws in workbook.worksheets:
    print ws.title
like image 167
Chris Lam Avatar answered Mar 25 '23 05:03

Chris Lam