想要使用Python Wincom32从工作簿中读取工作表

我想使用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 

你的代码有一些错误:
1)而不是excel.visible = 1应该是excel.Visible = 1
2)改为excel.Workbooks.open('<PATH TO EXCEL>')excel.Workbooks.Open('<PATH TO EXCEL>')
3)而不是wb.Sheets.countwb.Sheets.Count
4)而不是wb.Sheets[ 89 ].namewb.Sheets[ 89 ].Name

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

 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