【问题标题】:FlexTable output .docx horizontal table adjusted to narrow marginsFlexTable 输出 .docx 水平表格调整为窄边距
【发布时间】:2018-12-06 20:47:26
【问题描述】:

我有一个名为html_tableflextable 对象,我想将其直接放入word 文档中,采用水平布局,边距很窄。 我面临两个问题:

1) 小插图中建议的方法会产生额外的页面(表格前一页,表格后一页)。我认为这是一个已知问题,但不清楚如何解决它。
2) 我希望有较窄的边距,并且水平页面上的结果表格会自动适合页面。我想要这个,以便我可以使用尽可能多的页面打印表格。我目前的方法是手动打开文档,更改布局并在 Word 上选择“自动调整”。

这是我用来生成文档的代码。出于说明目的,我将使用mtcars 作为我的表,但真正的表的行数比mtcars 多。

html_table <- regulartable(mtcars)    
doc <- read_docx() %>%
      # Make it landscape
      body_end_section_continuous() %>%
      # Add the table
      body_add_flextable(value = html_table,
                         split = TRUE
                        ) %>%
      body_end_section_landscape()
    # Write the .docx
    print( doc, target = "my_table.docx" )

【问题讨论】:

    标签: r flextable


    【解决方案1】:

    在 Word 文档中,节仅在它们停止时才定义(我无法解释为什么会这样,但这就是底层 xml 的方式......)。如果前面的部分不是横向的,横向部分也需要分页符。

    自动调整 flextable,请使用函数autofit

    library(flextable)
    library(officer)
    library(magrittr)
    
    html_table <- regulartable(mtcars) %>% 
      autofit()
    
    doc <- read_docx() %>%
      body_add_flextable(value = html_table, split = TRUE) %>%
      body_end_section_landscape() %>% # a landscape section is ending here
      print( target = "my_table.docx" )
    

    如果您不想要额外的页面,您将需要一个默认页面方向为横向的模板。此外,您不需要任何代码来管理方向或边距。

    【讨论】:

      猜你喜欢
      • 2019-12-02
      • 2012-05-16
      • 1970-01-01
      • 2010-09-27
      • 1970-01-01
      • 1970-01-01
      • 2013-02-12
      • 2012-02-12
      • 2018-07-20
      相关资源
      最近更新 更多