Python Word客户端找不到.dotx

时间:2018-11-06 21:21:16

标签: python python-3.6

Python当前无法找到我的文件。

import os
import win32com.client 

word=win32com.client.Dispatch("Word.Application")
word.Documents.Open('Test_template')

我得到'Sorry we can't find your file'

我跑步时

import os
print(os.listdir('.'))

我可以看到'Test_template.dotx'

当我更改

word.Documents.Open('Test_template')

word.Documents.Open('Test_template.dotx')

我遇到同样的错误

2 个答案:

答案 0 :(得分:0)

我在这里看不到指定的目录(尽管os.listdir()返回该项似乎很令人鼓舞),但是尝试指定完整的目录路径,如下所示:

>>> import cwmp
>>>
>>> with open("sample.xml", "r") as f:
...   doc = f.read()

>>> xml = cwmp.CreateFromDocument(doc)

>>> xml.Header
<_soapenv.Header_ object at 0x6fffde73ad0>

>>> xml.Body
<_soapenv.Body_ object at 0x6fffdcb3390>

# I expect these to exist, but apparently they don't
>>> xml.Header.ID
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Header_' object has no attribute 'ID'

>>> xml.Header.HoldRequests
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Header_' object has no attribute 'HoldRequests'

>>> xml.Body.GetParameterNames
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Body_' object has no attribute 'GetParameterNames'

# I can see that they have child elements of the correct type
#   so why can't I access them by name?
>>> xml.Body.wildcardElements()
[<cwmp.CTD_ANON_8 object at 0x6fffdcb36d0>]

>>> cwmp.GetParameterNames()
<cwmp.CTD_ANON_8 object at 0x6fffdedba90>

>>> xml.Header.wildcardElements()
[<cwmp.CTD_ANON_56 object at 0x6fffdcb3310>, <cwmp.CTD_ANON_57 object at 0x6fffdcb34d0>]

>>> cwmp.ID("Foo")
<cwmp.CTD_ANON_56 object at 0x6fffde73250>

>>> cwmp.HoldRequests(True)
<cwmp.CTD_ANON_57 object at 0x6fffe025d90>

并再次检查文件名是否完全匹配-我肯定已经用一个应该小写或大写的字母击中了这些错误。

答案 1 :(得分:0)

问题最终是由于Citrix连接掩盖了真实的文件位置。因此,路径看起来正确但不正确。