【问题标题】:RUBY plain text to Docx with specific formatting具有特定格式的 RUBY 纯文本到 Docx
【发布时间】:2018-05-02 04:41:49
【问题描述】:

我经常需要制作非常标准的 Word 文档。某些参数的内容会发生变化,但它始终是预先编写的内容的混合。所以我决定编写一些 ruby​​ 代码来更容易地做到这一点,它在创建包含我需要的最终文本的 txt 文件时效果很好。

问题是我需要将此文本转换为 .docx 并具有特定格式。因此,我试图找到一种方法来在文本文件中指示哪些文本应该是粗体、斜体、具有不同的缩进或作为脚注,以便于解释(就像 html 一样)。例如:

<b>此文本应为粗体</b>
\t 缩进适用于制表符
<i>希望这可以是斜体</i>
<f>我希望这可以成为前一句的脚注</f>

但是,我无法做到这一点。

有人知道如何实现吗?我已经阅读了有关宏和 pandoc 的信息,但没有任何运气来实现这一点。对于宏来说似乎太复杂了。也许我正在尝试的不是最好的方法。也许使用 LaTeX 或创建 html 然后转换为 word? html可以创建脚注吗? (这似乎是最复杂的)

我不知道,我只是通过视频教程学习了Ruby,所以我的知识非常有限。

谢谢大家!

编辑:Arjun 的回答几乎解决了整个问题,但他指出的宝石不包括脚注的功能,不幸的是,脚注构成了我文件的很大一部分。因此,如果有人知道这样做的宝石,将不胜感激。谢谢!

【问题讨论】:

    标签: ruby formatting docx plaintext


    【解决方案1】:

    啊,Ruby 得到了宝石 ;)
    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 文件。不过我没试过。

    【讨论】:

    • 哇,谢谢,我查了一下,好像很有用。然而,就我所读到的而言,它并没有解决脚注的问题(我不得不经常使用它),这将是唯一缺少的东西。
    • 还有sablon。测试夹具在 docx 中有页脚和页眉。不过,您将不得不玩它。
    • 谢谢!我在 sablon 中找不到脚注功能(对不起,我真的是一个烦人的新手),但我发现了另一个名为 openxml-docx 的 gem,它说它支持脚注,我会玩一会儿。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2016-12-11
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-30
    • 1970-01-01
    相关资源
    最近更新 更多