java - 变量替换有时不起作用
问题描述
我将 docx 文件作为模板,在使用 Microsoft Word 编写变量(${})时,没有看到一些变量
但是当我在 LibreOffice 上更改它时,它正在工作(java 看到变量),但我不能每次都使用 LibreOffice!
File doc = new File("nameOfMyFile.docx");
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(doc);
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
HashMap mappings = new HashMap();
VariablePrepare.prepare(wordMLPackage);
mappings.put("lessonsEachWeek", contract.getHoursInWeek());
wordMLPackage.getMainDocumentPart().variableReplace(mappings);
Docx4J.save(wordMLPackage, new File("someName.docx"));
Docx 文件的 XML:
<w:r>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:eastAsia="Times New Roman" w:cs="Times New Roman"/>
<w:color w:val="000000"/>
<w:lang w:eastAsia="ru-RU"/>
</w:rPr>
<w:t xml:space="preserve">1.2 some text ${</w:t>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:eastAsia="Times New Roman" w:cs="Times New Roman"/>
<w:color w:val="000000"/>
<w:lang w:val="en-US" w:eastAsia="ru-RU"/>
</w:rPr>
<w:t>lessonsEachWeek</w:t>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:eastAsia="Times New Roman" w:cs="Times New Roman"/>
<w:color w:val="000000"/>
<w:lang w:eastAsia="ru-RU"/>
</w:rPr>
<w:t xml:space="preserve">} some text</w:t>
</w:r>
</w:p>
如您所见,括号是其他运行,但使用 LibreOffice 时:
<w:r>
<w:rPr>
<w:rFonts w:eastAsia="Times New Roman" w:cs="Calibri"/>
<w:b/>
<w:bCs/>
<w:color w:val="000000"/>
<w:lang w:eastAsia="ru-RU"/>
</w:rPr>
<w:t>${lessonsEachWeek}</w:t>
</w:r>
和错误:
2019-07-17 15:00:57.097 WARN 10717 --- [nio-8080-exec-5] org.docx4j.XmlUtils : Invalid key '</w:t></w:r><w:r><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:eastAsia="Times New Roman" w:cs="Times New Roman"/><w:color w:val="000000"/><w:lang w:val="en-US" w:eastAsia="ru-RU"/></w:rPr><w:t>lessonsEachWeek</w:t></w:r><w:r><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:eastAsia="Times New Roman" w:cs="Times New Roman"/><w:color w:val="000000"/><w:lang w:eastAsia="ru-RU"/></w:rPr><w:t xml:space="preserve">' or key not mapped to a value
2019-07-17 15:00:57.097 WARN 10717 --- [nio-8080-exec-5] org.docx4j.XmlUtils : Invalid key '</w:t></w:r><w:r><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:eastAsia="Times New Roman" w:cs="Times New Roman"/><w:color w:val="000000"/><w:lang w:val="en-US" w:eastAsia="ru-RU"/></w:rPr><w:t>lessonsEachWeek</w:t></w:r><w:r><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:eastAsia="Times New Roman" w:cs="Times New Roman"/><w:color w:val="000000"/><w:lang w:eastAsia="ru-RU"/></w:rPr><w:t xml:space="preserve">' or key not mapped to a value
2019-07-17 15:00:57.135 INFO 10717 --- [nio-8080-exec-5] o.d.o.parts.WordprocessingML.BinaryPart : .. closed.
解决方案
根本原因
Word 的文本校对功能已将lessonsEachWeek
文本包装到一些标签中,将其${
与标记分开}
。当然,给定这样的文档 docx4j 会尝试在</w:t></w:r><w:r><w:rPr>...lessonsEachWeek...</w:rPr><w:t xml:space="preserve">
没有这样的键的情况下找到键的替换。
解决方案
不幸的是,对于现有文档,我无法找到自动且优雅的方式来删除这些包装标签。由于某种原因,禁用 Word 中的拼写检查、重新检查和保存文档不起作用。我做了一些“手动手术”:在 7z 中打开 docx 文件作为 zip 文件,位于其中的 \word\document.xml,F4(编辑)并删除这些包装标签。7z 重新打包的 zip 文件,Word 仍然可以毫无问题地打开它,而我的 Java / Kotlin 应用程序现在可以替换已修复的密钥!任何类似的存档器和编辑器也应该可以做到这一点。
如何在新文档中避免这个问题
创建此类模板时禁用 Word 中的文本校对。
“文件”->“选项”->“校对”,取消选中“在 Word 中更正拼写和语法时”部分中的所有复选框。
推荐阅读
- mybatis - 像这样的 json 的 xml 映射是什么?
- javascript - JavaScript 计算问题
- php - 当我使用变量而不是硬编码数组时,为什么这个 laravel 测试数据库插入不起作用?
- python - 检测彼此最远的霍夫线?
- python - 将嵌套列表传递给 sklearn 拆分函数
- mql4 - 如何检查其他 EA 是否在 MQL4 中运行?
- angular - Angular 被动事件监听器
- python - 使用 RPLider (Python) 进行本地化
- tensorflow - 使用重用标志将 tf.variable_scope 转换为 tf.keras.layers.Layer
- c - 如何获取 shstrtab 的字符串?