【问题标题】:Read from a word file in python从python中的word文件中读取
【发布时间】:2020-07-23 15:44:35
【问题描述】:

如何在 python 中读取单词 (docx) 文件。我可以从 txt 文件中读取,但不能对 MS Office word 文档执行相同的操作。有什么建议吗?

【问题讨论】:

标签: python docx readlines


【解决方案1】:

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

  1. python-docx.

  2. docx2txt(请注意,它似乎不适用于.doc)。根据this,它似乎比 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") 
  1. textract(通过docx2txt工作)。

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

【讨论】:

  • 谢谢 doc2txt 解决了我的问题。我已经为此苦苦挣扎了很长时间。
【解决方案2】:

查看这个允许读取 docx 文件的库https://python-docx.readthedocs.io/en/latest/

您应该使用 PyPi 上提供的 python-docx 库。然后你可以使用以下

doc = docx.Document('myfile.docx')
allText = []
for docpara in doc.paragraphs:
    allText.append(docpara.text)

【讨论】:

  • 您好!虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
猜你喜欢
  • 1970-01-01
  • 2016-08-31
  • 2010-09-12
  • 1970-01-01
  • 1970-01-01
  • 2010-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多