Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract text inserted with track-changes in python-docx

I want to extract text from word documents that were edited in "Track Changes" mode. I want to extract the inserted text and ignore the deleted text.

Running the below code I saw that paragraphs inserted in "track changes" mode return an empty Paragraph.text

import docx

doc = docx.Document('C:\\test track changes.docx')

for para in doc.paragraphs:
    print(para)
    print(para.text)

Is there a way to retrieve the text in revisioned inserts (w:ins elements) ?

I'm using python-docx 0.8.6, lxml 3.4.0, python 3.4, Win7

Thanks

like image 569
yiftah Avatar asked Oct 27 '25 19:10

yiftah


1 Answers

I was having the same problem for years (maybe as long as this question existed).

By looking at the code of "etienned" posted by @yiftah and the attributes of Paragraph, I have found a solution to retrieve the text after accepting the changes.

The trick was to get p._p.xml to get the XML of the paragraph and then using "etienned" code on that (i.e retrieving all the <w:t> elements from the XML code, which contains both regular runs and <w:ins> blocks).

Hope it can help the souls lost like I was:

from docx import Document

try:
    from xml.etree.cElementTree import XML
except ImportError:
    from xml.etree.ElementTree import XML


WORD_NAMESPACE = "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}"
TEXT = WORD_NAMESPACE + "t"


def get_accepted_text(p):
    """Return text of a paragraph after accepting all changes"""
    xml = p._p.xml
    if "w:del" in xml or "w:ins" in xml:
        tree = XML(xml)
        runs = (node.text for node in tree.iter(TEXT) if node.text)
        # Note: on older versions it is `tree.getiterator` instead of `tree.iter`
        return "".join(runs)
    else:
        return p.text


doc = Document("Hello.docx")

for p in doc.paragraphs:
    print(p.text)
    print("---")
    print(get_accepted_text(p))
    print("=========")
like image 64
Jean-Francois T. Avatar answered Oct 29 '25 09:10

Jean-Francois T.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!