Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get excel sheet name in Python using xlrd

Tags:

python

xlrd

Please see the code below.

def getSheetName(file_name):
    pointSheetObj = []
    import xlrd as xl
    TeamPointWorkbook = xl.open_workbook(file_name)
    pointSheets = TeamPointWorkbook.sheet_names()

    for i in pointSheets:
        pointSheetObj.append(TeamPointWorkbook.sheet_by_name(i))

I need to get the name of the excel sheet name from the list pointSheetObjby iterating it.

like image 369
Anand Avatar asked Jun 12 '14 08:06

Anand


1 Answers

I have modified the code I gave as a question and have got what I needed actually,

def getSheetName(file_name):
    pointSheetObj = []
    import xlrd as xl
    TeamPointWorkbook = xl.open_workbook(file_name)
    pointSheets = TeamPointWorkbook.sheet_names()

    for i in pointSheets:
        pointSheetObj.append(tuple((TeamPointWorkbook.sheet_by_name(i),i)))

so if the list (of tuple) pointSheetObj is iterated we have name of the sheet at index 1 of the tuple inside the pointSheetObj.

By doing this I have got the name and the worksheet object with which I can carry on with other sheet related methods.

like image 100
Anand Avatar answered Nov 10 '22 12:11

Anand