Excel 导入期间 Python 中的 pywintypes.com_error

Pea*_*lip 4 python excel operating-system

我在运行模块 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)
Run Code Online (Sandbox Code Playgroud)

这是我的模块 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
Run Code Online (Sandbox Code Playgroud)

Nor*_*Cat 6

您可以使用此技术来获取有关错误的更多信息:

import win32api
e_msg = win32api.FormatMessage(-2147352565)
print e_msg.decode('CP1251')
Run Code Online (Sandbox Code Playgroud)

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

  • 嘿,我尝试使用您添加的代码,但在 'e_msg.decode('CP1251') 上出现语法错误。有什么建议? (4认同)