0

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 number from the program?

import xlwt
import xlrd
import csv

workbook = xlrd.open_workbook('input.xls')

worksheet = workbook.add_sheet('new_sheet')

1 Answer 1

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 sheet
print 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)
for index, sheet in enumerate(workbook.sheet_names()):
    if sheet == <name of your sheet>:
        print index

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

Regards

Sign up to request clarification or add additional context in comments.

6 Comments

Lets say I create the sheet new_sheet, how do i get the sheet number from that
With the second method. "workbook.sheet_names()" return a list with all sheets name
right but how do I get the sheet number. Ex: the first sheet's sheet number is 0, 2nd is 1, 3rd is 2
The index of list is the number of sheet
My bad I just did that, sorry
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.