如何向 Pandoc 生成的 docx 文件添加自定义格式?

cit*_*lao 18 docx pandoc

如何将自定义格式添加到Pandoc从 Markdown 生成的 docx 文件中?例如,我可以突出显示文本或将其居中吗?我可以重新设置链接样式吗?

我知道我可以通过使用--reference-doc(使用 生成)来更改默认元素(例如标题pandoc -o custom-reference.docx --print-default-data-file reference.docx)的样式,但我想引入其他样式。

cit*_*lao 33

在 Pandoc 中自定义 docx 输出基本上有 3 种方法:

  1. [问题中提到]通过编辑参考文档来自定义默认元素(如标题和块引用)。
  2. 通过在参考文档中编辑这些样式来自定义其他元素(例如超链接或默认段落字体)。
  3. 通过将自定义“元素”(样式)添加到参考文档来添加它们。

选项 1. 自定义默认元素

  1. Generate a reference doc (as mentioned in the question: pandoc -o custom-reference.docx --print-default-data-file reference.docx). It will have a bunch of content, representing common Pandoc components like headings and blockquotes (see picture at the end).

  2. In the Styles menu at the top of Word, find the style you want to change and edit it to be what you'd like (I'm not sure if you can simply edit the text's style directly).

Option 2: Customize other elements

There are more styles than the ones that appear in the reference document text and in the "Style gallery." For example, you can customize hyperlinks (e.g. color).

  1. Generate a reference doc (or use the one you have from above).

  2. Click the expand button in the Styles section to show the Styles pane (see below).

    样式溢出菜单

  3. Find the style you want, then edit it.

Also:

  • You can click an option in the dropdown beside each style to add it to the Style gallery, for easier editing.
  • You can add text with that style to the document itself, if you'd like to preview what it will look like in context.

Option 3: Add custom styles

This is probably the most relevant to the question. If you want to introduce custom elements beyond the default ones Pandoc provides and Word provides, you can simply add custom styles to the reference document. For example, you can create a style for highlighted text or centered text, or a style for large code blocks.

  1. Generate a reference doc (or use the one you have from above).

  2. Add some text to the document using the base style you want---the style you want to base your new custom style on. For example, if you want "normal text but highlighted," write some new text that uses the "Normal" or "Body text" style.

  3. Click the dropdown in the Styles gallery.

  4. Click Create a Style

  5. Name the style, whatever you'd like.

  6. Click Modify... and customize the style however you'd like. You can always get to this menu later by right-clicking the style in the gallery and clicking "Modify..."

  7. Save the document.

  8. In the text you will convert to docx, add divs and spans with the appropriate custom-style attribute whenever you want to consume it. For example, if you have a paragraph style called Super big and a character style called Highlighted text:

    <div custom-style="Super big">My super big text</div>
    
    Normal text. <span custom-style="Highlighted text">This is highlighted</span>.
    
    Run Code Online (Sandbox Code Playgroud)

    Pandoc will apply those custom styles for you!


References


The default reference doc: 默认参考文档

  • @Jepeme:对于段落样式,您必须使用`div`;对于字符样式,您必须使用“span”。 (2认同)