Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically insert comments into a Microsoft Word document?

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.

like image 390
mac Avatar asked Feb 20 '09 10:02

mac


People also ask

Can you import comments into Word?

Open a new Word file or an existing Word file where you would like to paste the comments. 11. Insert your cursor into the newly opened file and select Ctrl + V to paste all the comments. Alternately, you can right-click in the newly opened file and then select Paste.


1 Answers

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

like image 63
Aaron Digulla Avatar answered Oct 11 '22 00:10

Aaron Digulla