Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么pywintypes.com_error的信息不可读?;_Python_Win32com - Fatal编程技术网

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

Python 为什么pywintypes.com_error的信息不可读?;,python,win32com,Python,Win32com,在安装了pywin32-216.win32-py2.7的Python 2.7.2下,当我在windows上使用win32 COM模块处理Excel时,如下所示: >>> import win32com.client >>> xlsApp = win32com.client.Dispatch('Excel.Application') >>> xlsApp.Workbooks.Open(r'D:/test.xls') 我得到一个错误: Trac

在安装了pywin32-216.win32-py2.7的Python 2.7.2下,当我在windows上使用win32 COM模块处理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)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第8行,处于打开状态
pywintypes.com_错误:(-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',无,0,
-2146827284),无)
虽然信息不可读,但我不知道出了什么问题

在互联网上搜索后,我在以下网站上找到了一些有用的信息:

pywintypes.com_错误:(-2147352567,'发生异常',(0, 找不到u'Microsoft Office Excel',u“'test.xls”。请检查 文件名的拼写,并验证文件位置是否正确 正确。\n\n如果您试图从most列表中打开该文件 “文件”菜单上的“最近使用的文件”,确保该文件没有 已重命名、移动或删除。“,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>
>xlsApp.Workbooks.Open(r'D:/test.xls')
如果我得到了可读的错误提示,我会毫不困难地立即解决问题

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


我将感谢你的帮助

我想它是这样出现的,因为您正在远东(也许是中国?)的某个地方使用国际化设置,并且您正在命令提示符中使用Python。我不确定问题出在Python、命令提示符还是两者的结合上,但无论哪种方式,这两者的结合在非拉丁语国际化设置中都不能很好地工作

我建议改为使用,因为它似乎正确地支持Unicode字符。下面是我在IDLE中查看最后一个字符串时发生的情况。这篇文章对我来说没有任何意义,但它可能对你有帮助:

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”的文档已经打开。不能同时打开同名文件,无论它们是否在同一文件夹中。 要打开第二份文档,请关闭已经打开的文档,或者重新命名其中的一个文档。 >>>
我也经历过类似的错误。在我的特殊情况下,我尝试使用这样创建的临时文件:

fileprefix = 'Report'
filesuffix = '.xlsx'
filename = tempfile.gettempdir() + '\\' + fileprefix + filesuffix
xfile = tempfile.NamedTemporaryFile(suffix = filesuffix, prefix = fileprefix)
在命名的临时文件仍处于打开状态时,该名称是否可用于再次打开该文件,因平台而异(在Unix上可以使用该名称;在Windows NT或更高版本上不能使用该名称)

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

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

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