这里是新手程序员。我正在尝试学习如何打开/编辑/保存Word文档文件。我使用的是 Windows 7 64 位并且有 Microsoft Word 2010。我正在遵循本指南:https://python-docx.readthedocs.org/en/latest/user/quickstart.html
我按照第一部分“打开文档”输入前两行,但没有打开任何内容。所以我一定做错了什么。根本打不开话。有什么建议吗?
最佳答案
你想做什么...那就是创建一个新文档...而不是在Word中打开它
打开文件
os.startfile(r"C:\Some\Folder\doc1.docx")
docx(您引用的python模块)的用途是创建新的docx文件,然后您可以在创建它们后在word中打开它们
考虑以下因素
from docx import Document
import os
document = Document() #create a new document
paragraph = document.add_paragraph('Lorem ipsum dolor sit amet.')
document.save('test.docx') #save it to the filesystem
os.startfile("test.docx") #open it with the default handler for docx (usually word)
关于Python Docx Lib - 打开 Word,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26471014/