Skip to content Skip to sidebar Skip to footer

Python Excel - How To Turn Sheet Name Into Sheet Number

In this program I create a sheet on the input excel file called new_sheet. I need the sheet number of the sheet without having to look at the excel file. How do I return the sheet

Solution 1:

I'm not understand well if you want count how many sheets there are in your Excel file or if you want know, gave the name of sheet at which number correspond; anyway, if you want count how many sheets there are in an Excel file you can proceed in this way:

workbook = xlrd.open_workbook('input.xls')
worksheet = workbook.add_sheet('new_sheet')

# number of sheetprint workbook.nsheets

Instead, if you want know the corresponding number of sheets from the name you can proceed in this way:

workbook = xlrd.open_workbook('input.xls', on_demand=True)
forindex, sheet in enumerate(workbook.sheet_names()):
    if sheet == <name of your sheet>:
        printindex

With on_demand=True, you can open file not loading automatically.

Regards

Post a Comment for "Python Excel - How To Turn Sheet Name Into Sheet Number"