要使用Python Wincom32阅读工作簿中的工作表吗

要使用Python Wincom32阅读工作簿中的工作表吗,python,excel,Python,Excel,我想使用Python Wincom32模块从工作簿中读取工作表,但如果工作表的数量更多,则无法读取。 例如,我有一个excel工作簿,其中总共有150个工作表,我试图使用Python Wincom32模块阅读第89个excel工作表,但它给了我一些excel工作簿中根本不存在的工作表名称。 我正在使用下面的python代码 import win32com.client dispatch = win32com.client.Dispatch excel = dispatch("Excel.Appl

我想使用Python Wincom32模块从工作簿中读取工作表,但如果工作表的数量更多,则无法读取。 例如,我有一个excel工作簿,其中总共有150个工作表,我试图使用Python Wincom32模块阅读第89个excel工作表,但它给了我一些excel工作簿中根本不存在的工作表名称。 我正在使用下面的python代码

import win32com.client
dispatch = win32com.client.Dispatch
excel = dispatch("Excel.Application")
excel.visible = 1
wb = excel.Workbooks.open('<PATH TO EXCEL>')
count = wb.Sheets.count # count stores total number of worksheets present
print count  
ws_name = wb.Sheets[ 89 ].name 
""" This is supposed to read the name of 89th Worksheet instead it is giving me some garbage
    name  """
print ws_name
导入win32com.client
dispatch=win32com.client.dispatch
excel=分派(“excel.Application”)
excel.visible=1
wb=excel.Workbooks.open(“”)
count=wb.Sheets.count#count存储当前工作表的总数
打印计数
ws_name=wb.Sheets[89]。名称
“这应该是第89工作表的名字,但它给了我一些垃圾。”
名称“”
打印ws_名称

您的代码中有一些错误:
1) 相反,
excel.visible=1
应该是
excel.visible=1

2) 而是
excel.Workbooks.open(“”)
-
excel.Workbooks.open(“”)

3) 而是
wb.Sheets.count
-
wb.Sheets.count

4) 而是
wb.Sheets[89]。名称
-
wb.Sheets[89]。名称

这是固定版本(适用于我):

导入win32com.client
dispatch=win32com.client.dispatch
excel=分派(“excel.Application”)
excel.Visible=1
wb=excel.Workbooks.Open(')
count=wb.Sheets.count#count存储当前工作表的总数
打印计数
ws_name=wb.Sheets[89]。名称
“这应该是第89工作表的名字,但它给了我一些垃圾。”
名称“”
打印ws_名称

您的代码对我来说很好。你确定你的wb有150张工作表吗?count变量打印什么?是否确实打开了正确的工作簿?如果它给了你一些工作簿中没有的工作表名称,那一定是来自某个地方;我不认为Excel是在编造数据,因为你抓到它在玩游戏,它在boss屏幕上打开了。@sk11我的工作簿有150个工作表,count变量将输出作为Excel文件中的工作表总数。你是否在一个工作簿中使用了超过85个工作表来尝试上述代码;因为当我在ws_name=wb.Sheets[89].name中输入大于85的值时,我在阅读工作表名称时遇到了问题。name??是的,我试过了,效果很好。你能给一个链接到你的excel文件吗?现在它也为我工作;正如你在回答中提到的,这个python代码在我的代码中没有做任何更改。
import win32com.client
dispatch = win32com.client.Dispatch
excel = dispatch("Excel.Application")
excel.Visible = 1
wb = excel.Workbooks.Open('<PATH TO EXCEL>)
count = wb.Sheets.Count # count stores total number of worksheets present
print count
ws_name = wb.Sheets[ 89 ].Name
""" This is supposed to read the name of 89th Worksheet instead it is giving me some garbage
    name  """
print ws_name