Python Docx Lib - 打开 Word

标签 python docx

这里是新手程序员。我正在尝试学习如何打开/编辑/保存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/

相关文章:

python - 如何使用枚举解压元组列表?

python - 如何使用 PYTHONPATH 与 VSCode Python 扩展进行调试?

python - 我将如何对某些列取逐行平均值,同时将其他列保留在我的数据框中?

xml - 如何将 WordprocessingML 文件夹压缩成可读的 docx

Python docx Lib 居中对齐图像

c# - Novacode Docx 从位图创建图像

python - 求解器 'CPLEX' 在 CVXPY 中失败

python - Numpy 将矩阵 append 到张量

php - PHP finfo_file 中的 DOCX 文件类型是 application/zip

python - 使用 docx.Document() 时的回溯(最近一次调用最后一次)