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

前端 未结 3 2102
误落风尘
误落风尘 2021-01-23 03:19

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

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-23 04:11

    Not directly using python-docx; there's no API support yet for tracked changes/revisions.

    It's a pretty tricky job, which you'll discover if you search on the element names, perhaps 'open xml w:ins' for a start, that brings up this document as the first result: https://msdn.microsoft.com/en-us/library/ee836138(v=office.12).aspx

    If I needed to do something like that in a pinch I'd get the body element using:

    body = document._body._body
    

    and then use XPath on that to return the elements I wanted, something vaguely like this aircode:

    from docx.text.paragraph import Paragraph
    
    inserted_ps = body.xpath('./w:ins//w:p')
    for p in inserted_ps:
        paragraph = Paragraph(p, None)
        print(paragraph.text)
    

    You'll be on your own for figuring out what XPath expression will get you the paragraphs you want.

    opc-diag may be a friend in this, allowing you to quickly scan the XML of the .docx package. http://opc-diag.readthedocs.io/en/latest/index.html

提交回复
热议问题