如何以编程方式将注释插入Microsoft Word文档?

问题描述:

寻找一种以编程方式将注释(使用Word中的注释功能)插入MS Word文档中特定位置的方法.我希望这种方法可以在最新版本的MS Word标准格式中使用,并且可以在非Windows环境中实现(理想情况下使用Python和/或Common Lisp).我一直在寻找OpenXML SDK,但似乎找不到那里的解决方案.

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. 用单词创建一个简单的文档(即很小的文档)
  2. 在Word中添加评论
  3. 另存为docx.
  4. 使用python的zip模块访问存档(docx文件是ZIP存档).
  5. 将条目"word/document.xml"的内容转储到存档中.这是文档本身的XML.

这应该使您知道您需要做什么.之后,您可以使用Python中的XML库之一来解析文档,对其进行更改,然后将其添加回扩展名为".docx"的新ZIP存档中.只需从原始ZIP中复制所有其他条目,您就会获得一个新的有效Word文档.

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.

还有一个库可能会帮助您: openxmllib

There is also a library which might help: openxmllib