在Excel导入期间Python中的pywintypes.com_error

我得到这个错误在运行我的模块gasprop。 我不明白错误是什么意思,以及如何解决它:

import gasprop Traceback (most recent call last): File "<stdin>", line 1, in <module> File "gasprop.py", line 13, in <module> sheet = wb.Sheets("Input1") File "C:\Python27\lib\site-packages\win32com\gen_py\00020813-0000-0000-C000-000000000046x0x1x6\Sheets.py", line 113, in __call__ ret = self._oleobj_.InvokeTypes(0, LCID, 2, (9, 0), ((12, 1),),Index pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147352565), None) 

这里是我的模块gasprop:

 import win32com.client, os xl = win32com.client.gencache.EnsureDispatch("Excel.Application") thisdir = os.getcwd() wb = xl.Workbooks.Open(thisdir+"/Input1.xlsx") sheet = wb.Sheets("Input1") ...... def megaverify(self): listtr,listp=[],[] for i in range(16): tr=float(sheet.Range("D"+str(5+i)).Value) p=float(sheet.Range("C"+str(5+i)).Value) listtr.append(tr);listp.append(p) return tr, p 

你可以使用这个技巧来获得更多关于错误的信息:

 import win32api e_msg = win32api.FormatMessage(-2147352565) print e_msg.decode('CP1251') 

您收到的消息意味着您的Excel文件没有名称为"Input1" 。 你可以简单地重命名它。

发生错误是因为我想从Excel工作簿中调用的工作表与我在Python代码中引用它的名称不匹配。 我的表实际上是Sheet1(默认在Excel中),但是我正在调用一个表单Input1,如我的模块gasprop的第5行所示。 名称不匹配导致此错误。