如何在.docx XML中添加注释 [英] How to add comments to a .docx XML

查看:93
本文介绍了如何在.docx XML中添加注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在工作中,我们有一个Word文档,必须一直进行编辑以传递给另一个团队,以告诉他们如何执行某些任务.由于我不喜欢漫不经心地填写数据,并且我一直在寻找简化我必须完成的任务的方法,因此我决定将这一过程自动化.考虑了几种方法(例如从头开始生成Word文档或编辑现有文档)后,我决定就地编辑该文档.

At work, we have a word document that we have to edit all the time to pass on to another team, to tell them how to perform some tasks. Since I don't like mindlessly filling out data, and I always look for ways to simplify the tasks I have to do, I decided I would automate this process. After considering a few methods (such as generating a word document from scratch or editing an existing document), I decided to edit the document in-place.

我已经在文档中插入了特殊标记(具体来说,它们的格式为 [SOME_NAME_HERE] ),然后我将解析文档中的特殊标记,并将其替换为我实际需要的值.然后,将.docx提取到其中包含所有XML文档的文件夹中,并解析 document.xml 文件,替换值.

I have inserted special tags into the document (specifically, they take the form [SOME_NAME_HERE]), and I will then parse the document for those special tags and replace them with the value I actually need. I then extract the .docx to a folder with all of the XML documents inside of it, and parse the document.xml file, replacing the values.

在此过程中,根据实际需要,必须从文档中删除文档的某些部分.所以我首先想到的是在 document.xml 文件中添加注释.例如:

During this process, depending on what is actually needed, there are sections of the document that will have to be removed from it. So my first thought was to add comments to the document.xml file. For example:

<!-- INITIAL BUILD ONLY -->
      <w:p w:rsidR="00202319" w:rsidRPr="00D00FF5" w:rsidRDefault="00202319" w:rsidP="00AC0192">
        <w:r w:rsidR="00E548A2" w:rsidRPr="00D00FF5">
          <w:rPr>
            <w:rStyle w:val="emcfontstrong"/>
          </w:rPr>
          <w:t>Some text here</w:t>
        </w:r>
      </w:p>
<!-- END INITIAL BUILD ONLY -->

然后,当我要生成输出word文档时,我只需删除仅初始构建"的所有部分(当然,除非它是初始构建).

Then, when I go to generate the output word document, I would simply remove all of the sections that were "INITIAL BUILD ONLY" (unless, of course, it is the initial build).

但是,我遇到的问题是,当您将文档转换回Word文档时,在Word中打开并保存它,它将清理"文档,并删除我添加的所有注释

However, the issue I am running in to is that when you convert the document back to a Word document, open in Word and save it, it will "cleanup" the document, and remove all of the comments I've added to it.

所以,我的问题是,是否有任何方法可以在文档中保留注释,或者是否可以添加到XML的任何特殊标签,这些特殊标签在文档的标准查看/编辑过程中将不可见,但不会保存后被Word删除?

So, my question is, is there any way to preserve the comments in the document, or is there any special tags I could add to the XML that would not be visible during standard view/edit of the document, but would not be removed by Word upon a save?

推荐答案

选择编辑文档是imo的不错选择!

Choosing to edit the document is a good choice imo!

Word在打开docx并再次保存时做了很多更改,所以我不相信这一点.我什至不知道在 document.xml 中存储隐藏和持久数据的地方.

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}

名称为"edi9999"的

将替换为 Hello edi9999

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

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

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

将替换为:

Hello John
Hello Mary
Hello Jane

现在注释掉一个部分的技巧是使用一个空数组.

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

如果 names = []

输出将是一个空字符串.如果要取消注释,请使用具有一个元素的数组.

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

灵感来自胡子

我已经为此Javascript(在Node和浏览器中运行)创建了一个实现: https://github.com/edi9999/docxgenjs

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

这里有一个演示:

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

这篇关于如何在.docx XML中添加注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆