如何使用 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
How to get excel sheet name in Python using xlrd
提问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 pointSheetObj
by 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
) pointSheetObj
is iterated we have name of the sheet at index 1
of the tuple
inside the pointSheetObj
.
因此,如果(名单tuple
)pointSheetObj
迭代,我们在有表的名称index 1
的tuple
内部pointSheetObj
。
By doing this I have got the name and the worksheet object with which I can carry on with other sheet related methods.
通过这样做,我获得了名称和工作表对象,我可以使用这些名称和工作表对象继续使用其他与工作表相关的方法。