Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to get the comments and Corresponding content from a docx file ? #483

Open
smile-kindred opened this issue Mar 21, 2018 · 2 comments
Open

Comments

@smile-kindred
Copy link

smile-kindred commented Mar 21, 2018

<w:commentRangeStart w:id="0"/>
<w:r>...</w:r>
<w:commentRangeEnd w:id="0"/>

how to get <w:r>...</w:r> above

@zhnzhang
Copy link

Maybe you can use lxml.etree to have a try, like below:

from lxml import etree
import zipfile


ooXMLns = {'w': 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'}
docxFilePath = "Your Docx File Path."

docxZip = zipfile.ZipFile(docxFilePath)
documentXML = docxZip.read('word/document.xml')
et = etree.XML(documentXML)
commentRangeStarts = et.xpath('//w:commentRangeStart', namespaces=ooXMLns)

elem = commentRangeStarts[0].getnext()

getnext() will help you to get the element of <w:r>...</w:r>.

@regstuff
Copy link

For anyone coming to this from Google (like me), this worked for me:

def get_document_comments(docxFileName):
       comments_dict = {}
       comments_of_dict = {}
       docx_zip = zipfile.ZipFile(docxFileName)
       comments_xml = docx_zip.read('word/comments.xml')
       comments_of_xml = docx_zip.read('word/document.xml')
       et_comments = etree.XML(comments_xml)
       et_comments_of = etree.XML(comments_of_xml)
       comments = et_comments.xpath('//w:comment', namespaces=ooXMLns)
       comments_of = et_comments_of.xpath('//w:commentRangeStart', namespaces=ooXMLns)
       for c in comments:
          comment = c.xpath('string(.)', namespaces=ooXMLns)
          comment_id = c.xpath('@w:id', namespaces=ooXMLns)[0]
          comments_dict[comment_id] = comment
       for c in comments_of:
          comments_of_id = c.xpath('@w:id', namespaces=ooXMLns)[0]
          parts = et_comments_of.xpath(
            "//w:r[preceding-sibling::w:commentRangeStart[@w:id=" + comments_of_id + "] and following-sibling::w:commentRangeEnd[@w:id=" + comments_of_id + "]]",
            namespaces=ooXMLns)
          comment_of = ''
          for part in parts:
             comment_of += part.xpath('string(.)', namespaces=ooXMLns)
             comments_of_dict[comments_of_id] = comment_of
        return comments_dict, comments_of_dict

Courtesy: https://stackoverflow.com/a/75169632/3016570

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants