我试图使用win32com.client打开一个受密码保护的excel文件。
这个文件有37列28.140行。
我需要保持行数的动态,因为将来的文件将有不同的行数(或多或少)。我在这里找到了一个解决方案,到目前为止对较小的文件运行良好。
这是我的代码:
import win32com.client as w3c
xlApp = w3c.Dispatch("Excel.Application")
password = 'Greatpassword'
path = 'filepath'
xlwb = xlApp.Workbooks.Open(path,False, True, None, Password=password)
xlws = xlwb.Sheets[0]
max_clm = 37
xlUp = -4162
max_row = xlws.Cells(xlws.Rows.Count, "A").End(xlUp).Row
print(max_clm, max_row)
data = list(xlws.Range(xlws.Cells(1, 1), xlws.Cells(max_row, max_clm)).Value)
df = pd.DataFrame(data)
df.columns = df.iloc[0]
df = df.drop(0)
xlwb.Close()
xlApp.Quit()
这是我收到的错误消息:
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\win32com\client\dynamic.py", line 516, in __getattr__
ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147352566), None)
使用“win32api.FormatMessage(-2147352566)”可以得到如下文本:
"Out of present range."
这个错误信息是什么意思?我该如何处理它?
我重新检查了我的文件,以确保它具有行数(变量max_row)和列数(变量max_clm)。
print(max_clm, max_row)
给我37,28.140
提前谢谢!
比尔,
镁