在Pandoc表中添加样式规则以输出odt/docx(表边框) [英] Add styling rules in pandoc tables for odt/docx output (table borders)

查看:179
本文介绍了在Pandoc表中添加样式规则以输出odt/docx(表边框)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用knitr和pandoc通过markdown生成一些odt/docx报告,现在想知道如何格式化表格.首先,我对添加规则感兴趣(至少在标题的顶部,底部和下面,但是能够在表中添加任意规则也很好).

I'm generating some odt/docx reports via markdown using knitr and pandoc and am now wondering how you'd go about formating tables. Primarily I'm interested in adding rules (at least top, bottom and one below the header, but being able to add arbitrary ones inside the table would be nice too).

从pandoc文档到pandoc(不带任何特殊参数)运行以下示例,只会生成一个没有任何规则/颜色/指南(在-t odt-t docx中)的普通"表.

Running the following example from the pandoc documentation through pandoc (without any special parameters) just yields a "plain" table without any kind of rules/colours/guides (in either -t odt or -t docx).

+---------------+---------------+--------------------+
| Fruit         | Price         | Advantages         |
+===============+===============+====================+
| Bananas       | $1.34         | - built-in wrapper |
|               |               | - bright color     |
+---------------+---------------+--------------------+
| Oranges       | $2.10         | - cures scurvy     |
|               |               | - tasty            |
+---------------+---------------+--------------------+

我已经仔细检查了样式",以了解是否可以在参考.docx/.odt中指定表格式,但是除了表头"和表内容"样式外,什么都没有发现,这两种样式似乎都只涉及到表格中文本的格式.

I've looked through the "styles" for the possibility of specifying table formating in a reference .docx/.odt but found nothing obvious beyond "table header" and "table contents" styles, both of which seem to concern only the formatting of text within the table.

我对所见即所得(WYSIWYG)风格的文档处理器不甚了解,我对如何继续感到迷茫.

Being rather unfamiliar with WYSIWYG-style document processors I'm lost as to how to continue.

推荐答案

这是我搜索此操作的方法:

Here's how I searched how to do this:

在Docx中添加表的方法是使用<w:tbl>标记.因此,我在github存储库中搜索了此文件,并在此文件中中找到了它(称为Writers/Docx.hs,所以这并不奇怪)

The way to add a table in Docx is to use the <w:tbl> tag. So I searched for this in the github repository, and found it in this file (called Writers/Docx.hs, so it's not a big surprise)

blockToOpenXML opts (Table caption aligns widths headers rows) = do
  let captionStr = stringify caption
  caption' <- if null caption
                 then return []
                 else withParaProp (pStyle "TableCaption")
                      $ blockToOpenXML opts (Para caption)
  let alignmentFor al = mknode "w:jc" [("w:val",alignmentToString al)] ()
  let cellToOpenXML (al, cell) = withParaProp (alignmentFor al)
                                    $ blocksToOpenXML opts cell
  headers' <- mapM cellToOpenXML $ zip aligns headers
  rows' <- mapM (\cells -> mapM cellToOpenXML $ zip aligns cells)
           $ rows
  let borderProps = mknode "w:tcPr" []
                    [ mknode "w:tcBorders" []
                      $ mknode "w:bottom" [("w:val","single")] ()
                    , mknode "w:vAlign" [("w:val","bottom")] () ]
  let mkcell border contents = mknode "w:tc" []
                            $ [ borderProps | border ] ++
                            if null contents
                               then [mknode "w:p" [] ()]
                               else contents
  let mkrow border cells = mknode "w:tr" [] $ map (mkcell border) cells
  let textwidth = 7920  -- 5.5 in in twips, 1/20 pt
  let mkgridcol w = mknode "w:gridCol"
                       [("w:w", show $ (floor (textwidth * w) :: Integer))] ()
  return $
    [ mknode "w:tbl" []
      ( mknode "w:tblPr" []
        ( [ mknode "w:tblStyle" [("w:val","TableNormal")] () ] ++
          [ mknode "w:tblCaption" [("w:val", captionStr)] ()
          | not (null caption) ] )
      : mknode "w:tblGrid" []
        (if all (==0) widths
            then []
            else map mkgridcol widths)
      : [ mkrow True headers' | not (all null headers) ] ++
      map (mkrow False) rows'
      )
    ] ++ caption'

我对Haskell一点都不熟悉,但是我可以看到边框样式是硬编码的,因为其中没有变量:

I'm not familiar at all with Haskell, but I can see that the border-style is hardcoded, since there is no variable in it:

let borderProps = mknode "w:tcPr" []
                    [ mknode "w:tcBorders" []
                      $ mknode "w:bottom" [("w:val","single")] ()
                    , mknode "w:vAlign" [("w:val","bottom")] () ]

那是什么意思?

这意味着您不能使用当前版本的PanDoc更改docx表的样式. Howewer,有一种方法可以使您拥有自己的风格.

What does that mean ?

That means that you can't change the style of the docx tables with the current version of PanDoc. Howewer, there's a way to get your own style.

  1. 以所需的样式在表格上创建Docx文档(通过创建表格)
  2. 更改该文件的扩展名并解压缩
  3. 打开word/document.xml并搜索<w:tbl>
  4. 尝试找出您的样式如何转换为XML,并根据所见内容更改borderProps.
  1. Create a Docx Document with the style you want on your table (by creating that table)
  2. Change the extension of that file and unzip it
  3. Open word/document.xml and search for the <w:tbl>
  4. Try to find out how your style translates in XML and change the borderProps according to what you see.

这是我创建的带有边框样式的测试:

Here's a test with a border-style I created:

这是对应的XML:

<w:tblBorders>
  <w:top w:val="dotted" w:sz="18" w:space="0" w:color="C0504D" w:themeColor="accent2"/>
  <w:left w:val="dotted" w:sz="18" w:space="0" w:color="C0504D" w:themeColor="accent2"/>
  <w:bottom w:val="dotted" w:sz="18" w:space="0" w:color="C0504D" w:themeColor="accent2"/>
  <w:right w:val="dotted" w:sz="18" w:space="0" w:color="C0504D" w:themeColor="accent2"/>
  <w:insideH w:val="dotted" w:sz="18" w:space="0" w:color="C0504D" w:themeColor="accent2"/>
  <w:insideV w:val="dotted" w:sz="18" w:space="0" w:color="C0504D" w:themeColor="accent2"/>
</w:tblBorders>

那odt呢?

我还没有看过,请问您是否还没有使用类似的方法自己找到它.

What about odt ?

I didn't have a look at it yet, ask if you don't find by yourself using a similar method.

希望这会有所帮助,不要犹豫,再问其他问题

Hope this helps and don't hesitate to ask something more

这篇关于在Pandoc表中添加样式规则以输出odt/docx(表边框)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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