如何使用 xlrd 在 Python 中获取 Excel 工作表名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24179378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:08:55  来源:igfitidea点击:

How to get excel sheet name in Python using xlrd

pythonxlrd

提问by Anand

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.

我需要pointSheetObj通过迭代从列表中获取 Excel 工作表名称的名称。

采纳答案by Anand

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) pointSheetObjis iterated we have name of the sheet at index 1of the tupleinside the pointSheetObj.

因此,如果(名单tuplepointSheetObj迭代,我们在有表的名称index 1tuple内部pointSheetObj

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

通过这样做,我获得了名称和工作表对象,我可以使用这些名称和工作表对象继续使用其他与工作表相关的方法。