是否可以在Python中读取Word文件(.doc / .docx)

时间:2013-05-13 06:30:28

标签: python ms-word

我想创建一个验证工具;

任何人都可以帮我读取Python中的.doc / .docx文档,以便搜索和比较文件内容。

2 个答案:

答案 0 :(得分:7)

是的,这是可能的。 LibreOffice(至少)有一个命令行选项来转换可以处理的文件。使用它将文件转换为文本。然后按照例行操作将文本文件加载到Python中。

这对我在LibreOffice 4.2 / Linux上有用:

soffice --headless --convert-to txt:Text /path_to/document_to_convert.doc


我尝试了一些方法(包括odt2txt,antiword,zipfile,lpod,uno)。上面的soffice命令是第一个简单而无错误的命令。 This question使用ask.libreoffice.org上的soffice过滤器Try to upload the image in php server but it could post. in android帮助了我。

答案 1 :(得分:2)

你可以尝试使用PyWin32通过COM访问Word,虽然这会有点难看。你也可以看看IronPython,因为它是用.NET构建的,可能有更好的挂钩到Office。

另请参阅以下内容: