【发布时间】:2010-09-16 08:24:32
【问题描述】:
是否可以在不使用 COM 对象的情况下在 Python 中读取和写入 Word(2003 和 2007)文件?
我知道我可以:
f = open('c:\file.doc', "w")
f.write(text)
f.close()
但 Word 会将其读取为 HTML 文件而不是本机 .doc 文件。
【问题讨论】:
标签: python ms-word read-write
是否可以在不使用 COM 对象的情况下在 Python 中读取和写入 Word(2003 和 2007)文件?
我知道我可以:
f = open('c:\file.doc', "w")
f.write(text)
f.close()
但 Word 会将其读取为 HTML 文件而不是本机 .doc 文件。
【问题讨论】:
标签: python ms-word read-write
doc(本例中为 Word 2003)和 docx(Word 2007)是不同的格式,后者通常只是 xml 和图像文件的存档。我想通过操作这些 xml 文件的内容来写入 docx 文件是很有可能的。但是,我看不出没有某种类型的 COM 组件接口如何读取和写入 doc 文件。
【讨论】:
我会研究 IronPython,它本质上可以访问 windows/office API,因为它在 .NET 运行时上运行。
【讨论】:
见python-docx,其官方文档在here。
这对我来说效果很好。
【讨论】:
ValueError: file '<open file 'file.doc', mode 'r' at 0x7f29a1b5a6f0>' is not a Word file, content type is 'application/vnd.openxmlformats-officedocument.themeManager+xml
如果你只是看什么,那就simplest使用linux soffice命令将其转换为文本,然后将文本加载到python中:
【讨论】: