为什么 pywintypes.com_error 的信息不可读?
在安装了 pywin32-216.win32-py2.7 的 Python 2.7.2 下,当我使用 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, '发生异常。', (0, u'Microsoft Office Excel',u"'test .xls' 无法找到。检查 文件名的拼写,并验证文件位置是 正确。\n\n如果您尝试从最常使用的列表中打开该文件 文件菜单中最近使用过的文件,请确保该文件没有被删除 已重命名、移动或删除。”,u'C:\Program Files\Microsoft Office\OFFICE11\1033\xlmain11.chm', 0, -2146827284), 无)
我想这是同样的问题,所以我首先创建一个 Excel 文件 'D:/test.xls',然后一切都正常:
>>> xlsApp.Workbooks.Open(r'D:/test.xls')
<COMObject Open>
如果我得到可读的错误提示,我会立即解决问题,没有任何困难!
我想知道为什么我从 win32com.client 收到这样的错误?我可以做些什么来使信息可读吗?
我将感谢您的帮助!
Under Python 2.7.2 with pywin32-216.win32-py2.7 installed, when I use win32com module to process Excel on windows as follows:
>>> import win32com.client
>>> xlsApp = win32com.client.Dispatch('Excel.Application')
>>> xlsApp.Workbooks.Open(r'D:/test.xls')
I get an error:
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)
While the imformation is not readable, I don't konw what's going wrong!
After searching on the Internet, I find something helpful at 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)
I guess it's the same problem, so I create an Excel file 'D:/test.xls' first, and then everything turns ok:
>>> xlsApp.Workbooks.Open(r'D:/test.xls')
<COMObject Open>
If I got the readable error tip, I would solve the problem immediately without any difficult!
I wonder why is the error I get from win32com.client like that? Is there anything I can do to make the information readable?
I will appreciate your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想它之所以会这样,是因为您正在远东亚洲(也许是中国?)的某个地方使用国际化设置,并且您在命令提示符中使用Python。我不确定问题是否出在 Python、命令提示符或两者的组合上,但无论哪种方式,这两者的组合在非拉丁国际化设置中都不能很好地工作。
我建议使用 IDLE 代替,因为它似乎可以正确支持 Unicode 字符。以下是我在 IDLE 中查看最后一个字符串时发生的情况。该文本对我来说没有任何意义,但可能对您有任何意义:
但是,即使使用 IDLE,很可能当您遇到异常时,文本仍然会像上面那样显示。如果发生这种情况,您需要做的是从最后引发的异常中获取数据,并从其中
打印
相关字符串。要获取解释器中引发的最后一个异常,您可以使用
sys.last_value
。下面是一个带有不同异常消息的示例:I imagine that it has come out like that because you are using internationalization settings for somewhere in Far East Asia (China, perhaps?) and you're using Python in the Command Prompt. I'm not sure whether the problem is with Python, the Command Prompt or the combination of the two, but either way the combination of these two doesn't work particularly well with non-Latin internationalization settings.
I'd recommend using IDLE instead, since that appears to support Unicode characters properly. Here's what happens when I viewed that last string in IDLE. The text doesn't mean anything to me, but it might do to you:
However, even when using IDLE, chances are that when you get the exception, the text will still appear as it did above. If this happens, what you need to do is to get the data from the last exception raised and
print
the relevant string from within it.To get the last exception raised in the interpreter, you can use
sys.last_value
. Here's an example with a different exception message:我遇到了类似的错误。在我的特定情况下,我尝试使用这样创建的临时文件:
当指定的临时文件仍然打开时,该名称是否可以用于再次打开该文件,因平台而异(可以在 Unix 上使用) ; 它不能在 Windows NT 或更高版本上)。
所以,我似乎无法在 Windows 操作系统中重复使用该文件。这就是我收到错误的原因。
对于您的情况,您可能需要首先检查文件是如何创建的。
I experienced a similar error. In my particular case I was trying to use a temporary file created like this:
Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later).
So, it seems that I cannot re-use that file in Windows OS. And that is the reason that I am getting the error.
In your case, you may want to check how the file is being created in the first place.