简体   繁体   English

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

[英]Combine multiple docx with columns in officer R

I am trying to use the officer package in R to create multiple outputs with columns and then combine the outputs into a single document.我正在尝试使用 R 中的officer package 来创建带有列的多个输出,然后将输出合并到一个文档中。 I am able to create the initial outputs with columns, but when I combine them with body_add_docx() the column formatting goes away.我可以使用列创建初始输出,但是当我将它们与body_add_docx()结合使用时,列格式就会消失。 Wondering if I am missing something with my implementation.想知道我的实施是否遗漏了一些东西。

 # 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")

The final output does not have the same column structure as the inputs.最终的 output 没有与输入相同的列结构。

A thread is already available here: https://github.com/davidgohel/officer/issues/431此处已提供一个线程: https://github.com/davidgohel/officer/issues/431

Word does not keep the sections of the embedded documents as is. Word 不会按原样保留嵌入文档的各个部分。 officer just adds it and does not change the file.官员只是添加它而不更改文件。 It seems to me it is not possible to add a docx into another docx and preserve the original sections, they must be defined in the target/final document as when you use Word MANUALLY.在我看来,不可能将 docx 添加到另一个 docx 并保留原始部分,它们必须在目标/最终文档中定义,就像您手动使用 Word 时一样。

The section should be added/configured in the final/main document:该部分应在最终/主文档中添加/配置:

 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