繁体   English   中英

将多个docx与officer R中的列结合起来

[英]Combine multiple docx with columns in officer R

我正在尝试使用 R 中的officer package 来创建带有列的多个输出,然后将输出合并到一个文档中。 我可以使用列创建初始输出,但是当我将它们与body_add_docx()结合使用时,列格式就会消失。 想知道我的实施是否遗漏了一些东西。

 # sample text str1 <- "Lorem ipsum dolor sit amet, consectetur adipiscing elit." str1 <- rep(str1, 20) str1 <- paste(str1, collapse = " ") # Create initial section with columns x <- read_docx() x <- body_add_par(x, str1, style = "Normal") %>% body_end_section_columns(widths = c(3, 3), space = 0, sep = FALSE) print(x, target = "tmp1.docx") # Create another output with columns x <- read_docx() x <- body_add_par(x, str1, style = "Normal") %>% body_end_section_columns(widths = c(3, 3), space = 0, sep = FALSE) print(x, target = "tmp2.docx") # Combine the rendered sections together read_docx(path = "tmp1.docx") %>% body_add_break() %>% body_add_docx(src = "tmp2.docx") %>% print(target = "final.docx")

最终的 output 没有与输入相同的列结构。

此处已提供一个线程: https://github.com/davidgohel/officer/issues/431

Word 不会按原样保留嵌入文档的各个部分。 官员只是添加它而不更改文件。 在我看来,不可能将 docx 添加到另一个 docx 并保留原始部分,它们必须在目标/最终文档中定义,就像您手动使用 Word 时一样。

该部分应在最终/主文档中添加/配置:

 library(officer) file1 <- "file1.docx" file2 <- "file2.docx" file3 <- "file3.docx" x <- read_docx() x <- body_add_par(x, "hello world 1", style = "Normal") print(x, target = file1) x <- read_docx() x <- body_add_par(x, "hello world 2", style = "Normal") print(x, target = file2) ## First portrait, then landscape psportrait <- prop_section( page_size = page_size(orient = "portrait") ) pslandscape <- prop_section( page_size = page_size(orient = "landscape") ) # solution 1: keep default section as portrait x <- read_docx(path = file1) x <- body_end_block_section(x, value = block_section(pslandscape)) x <- body_add_docx(x, src = file2) x <- body_end_block_section(x, value = block_section(psportrait)) print(x, target = file3)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM