How to add comments to a .docx XML

匆匆过客 提交于 2019-12-02 04:03:07

Choosing to edit the document is a good choice imo!

Word does a lot of changes when opening a docx and saving it again, so I would'nt trust this. I don't even know where it would be possible to store hidden and persistent data inside document.xml.

Here's an idea to get what you need using an other technique.

Hello {name}

with name="edi9999" will be replaced by Hello edi9999

{#names}
Hello {name}
{/names}

with names=[{name:"John"},{name:"Mary"},{name:"Jane"}]

will be replaced by:

Hello John
Hello Mary
Hello Jane

Now the trick to comment out a section is to use an empty array.

if names=[]

the output will be an empty string. If you want to uncomment it, use an array with one element.

Inspired by Mustache

How do I build this ?

I have created an implementation of this for Javascript (works on Node and in Browser): https://github.com/edi9999/docxgenjs

There's a demo Here:

http://javascript-ninja.fr/docxgenjs/examples/demo.html

To echo the previous poster, I've found that Microsoft Word does a lot of behind-the-scenes magic when opening/saving files, and wouldn't suggest storing metadata in any xml files.

However, if you aren't above a bit of scripting, it shouldn't be too difficult to accomplish using any number of modules. Here's a basic Python implementation using oodocx, a module that I'm currently developing.

from oodocx import oodocx
from lxml import etree

d = oodocx.Docx('template.docx')
body = d.get_body()
paragraph_to_remove = d.search('Some text here', result_type='paragraph')
body.remove(paragraph_to_remove)
d.save('new document.docx')
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!