首页 > 解决方案 > Python word 客户端找不到 .dotx

问题描述

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')

我犯了同样的错误

标签: pythonpython-3.6

解决方案


我没有看到此处指定的目录(尽管 os.listdir() 返回该项目的事实似乎令人鼓舞),但请尝试指定完整的目录路径,如下所示:

word.Documents.Open('//folder_1/folder_2/Test_template.dotx')

# You could also specify the working directory explicitly with os, as in:
os.chdir('//folder_1/folder_2')

并仔细检查文件名是否完全匹配——我肯定用一个应该是小写字母而不是大写字母的字母遇到了这些错误。


推荐阅读