簡體   English   中英

如何使用 python-docx 從現有的 docx 文件中提取文本

[英]How to extract text from an existing docx file using python-docx

我正在嘗試使用python-docx模塊( pip install python-docx ),但它似乎非常混亂,因為在github 回購測試示例中,他們使用的是opendocx function,但在readthedocs中,他們使用的是Document class。即使它們只顯示如何將文本添加到 docx 文件,而不是讀取現有文件?

第一個( opendocx )不起作用,可能已被棄用。 對於第二種情況,我試圖使用:

from docx import Document

document = Document('test_doc.docx')
print(document.paragraphs)

它返回了<docx.text.Paragraph object at 0x... >的列表

然后我做了:

for p in document.paragraphs:
    print(p.text)

它返回了所有文本,但幾乎沒有遺漏任何東西。 所有 URL(CTRL+CLICK 到 go 到 URL)都沒有出現在控制台的文本中。

問題是什么? 為什么缺少網址?

我如何在不遍歷循環的情況下獲得完整的文本(類似於open().read()

你可以試試這個

import docx

def getText(filename):
    doc = docx.Document(filename)
    fullText = []
    for para in doc.paragraphs:
        fullText.append(para.text)
    return '\n'.join(fullText)

您可以使用改編自 python-docx 的python-docx2txt ,但也可以從鏈接、頁眉和頁腳中提取文本。 它還可以提取圖像。

你也可以試試這個

from docx import Document

document = Document('demo.docx')
for para in document.paragraphs:
    print(para.text)

不安裝python-docx

docx基本上是一個 zip 文件,其中包含多個文件夾和文件。 在下面的鏈接中,您可以找到一個簡單的函數來從docx文件中提取文本,而無需依賴python-docxlxml ,后者有時難以安裝:

http://etienned.github.io/posts/extract-text-from-word-docx-simply/

python-docx有兩個“世代”。 最初的一代以 0.2.x 版本結束,“新一代”開始於 v0.3.0。 新一代是對舊版本的徹底的、面向對象的重寫。 它有一個位於此處獨特存儲庫

opendocx() 函數是舊 API 的一部分。 該文檔適用於新版本。 舊版本沒有文檔可言。

當前版本不支持讀取和寫入超鏈接。 該功能在路線圖上,該項目正在積極開發中。 事實證明,它是一個相當廣泛的 API,因為 Word 具有如此多的功能。 所以我們會解決這個問題,但可能不會在下個月,除非有人決定專注於這方面並做出貢獻。 在此答案之后添加了更新超鏈接支持。

使用 python-docx,如@Chinmoy Panda 的回答所示:

for para in doc.paragraphs:
    fullText.append(para.text)

但是, para.text會丟失w:smarttag para.text的文本(相應的 github 問題在這里: https : //github.com/python-openxml/python-docx/issues/328 ),您應該使用以下函數:

def para2text(p):
    rs = p._element.xpath('.//w:t')
    return u" ".join([r.text for r in rs])

我有一個類似的問題,所以我找到了一個解決方法(由於正則表達式刪除超鏈接標簽,以便只保留一個段落標簽)。 我在https://github.com/python-openxml/python-docx/issues/85 BP 上發布了這個解決方案

這個問題似乎沒有官方解決方案,但是這里發布了一個解決方法https://github.com/savoirfairelinux/python-docx/commit/afd9fef6b2636c196761e5ed34eb05908e582649

只需更新此文件“...\\site-packages\\docx\\oxml_ init _.py”

# add
import re
import sys

# add
def remove_hyperlink_tags(xml):
    if (sys.version_info > (3, 0)):
        xml = xml.decode('utf-8')
    xml = xml.replace('</w:hyperlink>', '')
    xml = re.sub('<w:hyperlink[^>]*>', '', xml)
    if (sys.version_info > (3, 0)):
        xml = xml.encode('utf-8')
    return xml
    
# update
def parse_xml(xml):
    """
    Return root lxml element obtained by parsing XML character string in
    *xml*, which can be either a Python 2.x string or unicode. The custom
    parser is used, so custom element classes are produced for elements in
    *xml* that have them.
    """
    root_element = etree.fromstring(remove_hyperlink_tags(xml), oxml_parser)
    return root_element

當然不要忘記在使用的文檔中提到正在更改官方庫

暫無
暫無

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

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