我经常需要生成非常标准的 Word 文档。内容会根据某些参数而变化,但它始终是预先编写的内容的混合。因此,我决定编写一些 ruby 代码来更轻松地完成此操作,并且它在使用我需要的最终文本创建 txt 文件方面效果很好。
问题是我需要将此文本转换为 .docx
并具有特定的格式。因此,我试图找到一种方法来指示文本文件中哪些文本应该是粗体、斜体、具有不同的缩进,或者是脚注,以使其易于解释(就像 html 那样)。例如:
<b>
此文本应为粗体 </b>
\t
缩进与制表符一起使用
<i>
希望这可以是斜体 </i>
<f>
我希望这可以成为前一个短语 </f>
的脚注
但是,我没能做到这一点。
有人知道如何实现这一目标吗?我读过有关宏和 pandoc 的内容,但没有运气实现这一点。对于宏来说似乎太复杂了。也许我正在尝试的并不是最好的方法。也许使用 LaTeX 或创建 html 然后转换为 word? html可以创建脚注吗? (这似乎是最复杂的)
我不知道,我只是通过视频教程学习了 Ruby,所以我的知识非常有限。
谢谢大家!
编辑:阿琼的回答几乎解决了整个问题,但他指出的 gem 不包括脚注的功能,不幸的是,脚注构成了我文档的很大一部分。因此,如果有人知道有这样的 gem ,将不胜感激。谢谢!
最佳答案
啊,Ruby 为此获得了 gem ;)
https://github.com/trade-informatics/caracal
这将帮助您从 Ruby 代码本身编写文档。
来自自述文件
docx.p 'this text should be bold' do
style 'custom_style' # sets the paragraph style. generally used at the exclusion of other attributes.
align :left # sets the alignment. accepts :left, :center, :right, and :both.
color '333333' # sets the font color.
size 32 # sets the font size. units in 1/2 points.
bold true # sets whether or not to render the text with a bold weight.
italic false # sets whether or not render the text in italic style.
underline false # sets whether or not to underline the text.
bgcolor 'cccccc' # sets the background color.
vertical_align 'superscript' # sets the vertical alignment.
end
还有这个 gem ,https://github.com/nickfrandsen/htmltoword ,它将纯 html 转换为 doc 文件。不过我还没试过。
关于RUBY 纯文本到具有特定格式的 Docx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50113336/