从python中的word文件中读取

Sin*_*giz 2 python docx readlines

如何从python中的单词(docx)文件中读取。我可以读取 txt 文件,但不能读取 MS Office word 文档。有什么建议?

san*_*ica 5

有几个包可以让你做到这一点。查看

  1. 蟒蛇-docx

  2. docx2txt(请注意,它似乎不适用于.doc)。按照这个,它似乎比 python-docx 获得更多的信息。来自原始文档:

import docx2txt

# extract text
text = docx2txt.process("file.docx")

# extract text and write images in /tmp/img_dir
text = docx2txt.process("file.docx", "/tmp/img_dir") 
Run Code Online (Sandbox Code Playgroud)
  1. textract(通过docx2txt 工作)。

  2. 由于.docx文件只是.zip具有更改扩展名的文件,显示了如何访问内容。这是与.doc文件的显着差异,也是上述部分(或全部)不适用于.docs 的原因。在这种情况下,您可能必须先转换doc-> docxantiword是一种选择。