簡體   English   中英

從 python 中的 word 文件讀取

[英]Read from a word file in python

如何從 python 中的單詞(docx)文件中讀取。 我可以從 txt 文件中讀取,但不能對 MS Office word 文檔執行相同的操作。 有什么建議么?

有幾個包可以讓你做到這一點。 查看

  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") 
  1. textract (通過docx2txt工作)。

  2. 由於.docx文件只是具有更改擴展名的.zip文件, 因此這顯示了如何訪問內容。 這是與.doc文件的顯着差異,也是上述部分(或全部)不適用於.doc的原因。 在這種情況下,您可能必須先轉換doc -> docx antiword是一種選擇。

請參閱允許讀取 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM