为什么pywintypes.com_error的信息不可读

2024-09-30 01:35:27 发布

您现在位置:Python中文网/ 问答频道 /正文

在Python 2.7.2下,安装了pywin32-216.win32-py2.7,当我使用win32com模块在windows上处理Excel时,如下所示:

>>> import win32com.client
>>> xlsApp = win32com.client.Dispatch('Excel.Application')
>>> xlsApp.Workbooks.Open(r'D:/test.xls')

我得到一个错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<COMObject <unknown>>", line 8, in Open
pywintypes.com_error: (-2147352567, '\xb7\xa2\xc9\xfa\xd2\xe2\xcd\xe2\xa1\xa3',
(0, u'Microsoft Office Excel', u'\u540d\u4e3a\u201ctest.xls\u201d\u7684\u6587\u6
863\u5df2\u7ecf\u6253\u5f00\u3002\u4e0d\u80fd\u540c\u65f6\u6253\u5f00\u540c\u540
d\u6587\u4ef6\uff0c\u65e0\u8bba\u5b83\u4eec\u662f\u5426\u5728\u540c\u4e00\u6587\
u4ef6\u5939\u4e2d\u3002\n\u8981\u6253\u5f00\u7b2c\u4e8c\u4efd\u6587\u6863\uff0c\
u8bf7\u5173\u95ed\u5df2\u7ecf\u6253\u5f00\u7684\u6587\u6863\uff0c\u6216\u8005\u9
1cd\u65b0\u547d\u540d\u5176\u4e2d\u7684\u4e00\u4e2a\u6587\u6863\u3002', None, 0,
 -2146827284), None)

虽然信息不可读,但我不知道出了什么问题!

在网上搜索之后,我在http://www.python-forum.org/pythonforum/viewtopic.php?f=15&t=17665找到了一些有用的东西:

pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Office Excel', u"'test .xls' could not be found. Check the spelling of the file name, and verify that the file location is correct.\n\nIf you are trying to open the file from your list of most recently used files on the File menu, make sure that the file has not been renamed, moved, or deleted.", u'C:\Program Files\Microsoft Office\OFFICE11\1033\xlmain11.chm', 0, -2146827284), None)

我想是同样的问题,所以我先创建一个Excel文件'D:/test.xls',然后一切正常:

>>> xlsApp.Workbooks.Open(r'D:/test.xls')
<COMObject Open>

如果我得到可读的错误提示,我会立即解决问题,没有任何困难!

我想知道为什么从win32com.client得到的错误是这样的?我能做些什么使这些信息可读吗?

谢谢你的帮助!


Tags: thetestclient错误openxlsexcelmicrosoft
2条回答

我想这是因为你在远东某个地方(也许是中国)使用国际化设置在命令提示符下使用Python。我不确定问题出在Python、命令提示符还是两者的组合上,但无论哪种方式,这两者的组合在非拉丁国际化设置中都不是特别好。

我建议改用IDLE,因为它似乎正确地支持Unicode字符。下面是我在空闲状态下查看最后一个字符串时发生的情况。这段文字对我没有任何意义,但可能对你有影响:

IDLE 2.6.4      
>>> z = u'\u540d\u4e3a\u201ctest.xls\u201d\u7684\u6587\u6863\u5df2\u7ecf\u6253\u5f00\u3002\u4e0d\u80fd\u540c\u65f6\u6253\u5f00\u540c\u540d\u6587\u4ef6\uff0c\u65e0\u8bba\u5b83\u4eec\u662f\u5426\u5728\u540c\u4e00\u6587\u4ef6\u5939\u4e2d\u3002\n\u8981\u6253\u5f00\u7b2c\u4e8c\u4efd\u6587\u6863\uff0c\u8bf7\u5173\u95ed\u5df2\u7ecf\u6253\u5f00\u7684\u6587\u6863\uff0c\u6216\u8005\u91cd\u65b0\u547d\u540d\u5176\u4e2d\u7684\u4e00\u4e2a\u6587\u6863\u3002'
>>> print z
名为“test.xls”的文档已经打开。不能同时打开同名文件,无论它们是否在同一文件夹中。
要打开第二份文档,请关闭已经打开的文档,或者重新命名其中的一个文档。
>>> 

然而,即使使用IDLE,当您得到异常时,文本仍会像上面那样出现。如果发生这种情况,您需要做的是从上次引发的异常中获取数据,并从中获取相关字符串。

要获取解释器中引发的最后一个异常,可以使用^{}。下面是一个带有不同异常消息的示例:

>>> import sys
>>> with open('nonexistent.txt') as f: pass
...
Traceback (most recent call last):
  File "", line 1, in 
    with open('nonexistent.txt') as f: pass
IOError: [Errno 2] No such file or directory: 'nonexistent.txt'
>>> sys.last_value
IOError(2, 'No such file or directory')
>>> print(sys.last_value[1])
No such file or directory
>>> 

我也经历过类似的错误。在我的特殊情况下,我试图使用这样创建的临时文件:

fileprefix = 'Report'
filesuffix = '.xlsx'
filename = tempfile.gettempdir() + '\\' + fileprefix + filesuffix
xfile = tempfile.NamedTemporaryFile(suffix = filesuffix, prefix = fileprefix)

在命名的临时文件仍处于打开状态时,是否可以使用该名称再次打开该文件,这在不同的平台上有所不同(可以在Unix上使用,在Windows NT或更高版本上不能使用)。

所以,我似乎无法在Windows操作系统中重用该文件。这就是我出错的原因。

在您的情况下,您可能希望首先检查文件是如何创建的。

相关问题 更多 >

    热门问题