How to programmatically insert comments into a Microsoft Word document?

后端 未结 2 1424
甜味超标
甜味超标 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 00:58

    If this is server side (non-interactive) use of the Word application itself is unsupported (but I see this is not applicable). So either take that route or use the OpenXML SDK to learn the markup needed to create a comment. With that knowledge it is all about manipulating data.

    The .docx format is a ZIP of XML files with a defines structure, so mostly once you get into the ZIP and get the right XML file it becomes a matter of modifying an XML DOM.

    The best route might be to take a docx, copy it, add a comment (using Word) to one, and compare. A diff will show you the kind of elements/structures you need to be looking up in the SDK (or ISO/Ecma standard).

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题