How to programmatically insert comments into a Microsoft Word document?

后端 未结 2 1425
甜味超标
甜味超标 2021-02-06 00:16

Looking for a way to programmatically insert comments (using the comments feature in Word) into a specific location in a MS Word document. I would prefer an approach that is usa

2条回答
  •  甜味超标
    2021-02-06 01:02

    Here is what I did:

    1. Create a simple document with word (i.e. a very small one)
    2. Add a comment in Word
    3. Save as docx.
    4. Use the zip module of python to access the archive (docx files are ZIP archives).
    5. Dump the content of the entry "word/document.xml" in the archive. This is the XML of the document itself.

    This should give you an idea what you need to do. After that, you can use one of the XML libraries in Python to parse the document, change it and add it back to a new ZIP archive with the extension ".docx". Simply copy every other entry from the original ZIP and you have a new, valid Word document.

    There is also a library which might help: openxmllib

提交回复
热议问题