[英]Open a word document with python using windows
I am trying to open a word document with python in windows, but I am unfamiliar with windows.我试图在windows中用python打开一个word文档,但我对windows不熟悉。
My code is as follows.我的代码如下。
import docx as dc
doc = dc.Document(r'C:\Users\justin.white\Desktop\01100-Allergan-UD1314-SUMMARY OF WORK.docx')
Through another post, I learned that I had to put the r in front of my string to convert it to a raw string or it would interpret the \\U as an escape sequence.通过另一篇文章,我了解到必须将 r 放在字符串前面才能将其转换为原始字符串,否则它会将 \\U 解释为转义序列。
The error I get is我得到的错误是
PackageNotFoundError: Package not found at 'C:\Users\justin.white\Desktop\01100-Allergan-UD1314-SUMMARY OF WORK.docx'
I'm unsure of why it cannot find my document, 01100-Allergan-UD1314-SUMMARY OF WORK.docx.我不确定为什么它找不到我的文档 01100-Allergan-UD1314-SUMMARY OF WORK.docx。 The pathway is correct as I copied it directly from the file system.
路径是正确的,因为我直接从文件系统复制了它。
Any help is appreciated thanks.感谢任何帮助。
try this试试这个
import StringIO
from docx import Document
file = r'H:\myfolder\wordfile.docx'
with open(file) as f:
source_stream = StringIO(f.read())
document = Document(source_stream)
source_stream.close()
http://python-docx.readthedocs.io/en/latest/user/documents.html http://python-docx.readthedocs.io/en/latest/user/documents.html
Also, in regards to debugging the file not found error, simplify your directory names and files names.此外,关于调试文件未找到错误,请简化您的目录名称和文件名称。 Rename the file to 'file' instead of referring to a long path with spaces, etc.
将文件重命名为 'file' 而不是引用带有空格等的长路径。
If you want to open the document in Microsoft Word try using os.startfile()
.如果您想在 Microsoft Word 中打开文档,请尝试使用
os.startfile()
。
In your example it would be:在您的示例中,它将是:
os.startfile(r'C:\Users\justin.white\Desktop\01100-Allergan-UD1314-SUMMARY OF WORK.docx')
This will open the document in word on your computer.这将在您的计算机上以 word 格式打开文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.