How to programmatically insert comments into a Microsoft Word document?

青春壹個敷衍的年華 提交于 2019-12-03 17:15:22

问题


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 usable across recent versions of MS Word standard formats and implementable in a non-Windows environment (ideally using Python and/or Common Lisp). I have been looking at the OpenXML SDK but can't seem to find a solution there.


回答1:


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




回答2:


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).



来源:https://stackoverflow.com/questions/568972/how-to-programmatically-insert-comments-into-a-microsoft-word-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!