使用“军官”包装插入外部docx

时间:2018-06-27 15:12:32

标签: r officer

为什么在“官员”包中的body_add_docx方法不起作用?我在哪里弄错了?

library(officer)
library(magrittr)

read_docx(path = "/home/user/page1.docx") %>% # load page1.docx as base document
  body_add_break() %>% # add page break
  body_add_docx(src="/home/user/page2.docx") %>% #FIXME: This method doesn't work
  print(target = "/home/user/out.docx") # out.docx conteins only page1.docx !?

2 个答案:

答案 0 :(得分:0)

下面的代码仅适用于Windows,MS Word,并且没有分页符。

对于Linux,LibreOffice,Google文档,它不起作用。

library(officer)
library(magrittr)

read_docx(path = "/home/user/page1.docx") %>%
  # body_add_break() %>% # with page break it doesn't work
  body_add_docx(src="/home/user/page2.docx") %>% # only for Widows and MS Word
  print(target = "/home/user/out.docx") 

答案 1 :(得分:0)

函数body_add_docx正在使用MS Word功能。编辑文档时,文件的内容会复制到主文档中,但这仅在用Word编辑文档时才会发生。 LibreOffice和gdoc可能未实现此功能(至少我不了解它们)。

以下脚本仅在使用Word编辑时才生成预期的文档:

library(officer)
library(magrittr)

read_docx() %>% 
  body_add_par("hello world 1", style = "Normal") %>% 
  print(target = "doc1.docx")
read_docx() %>% 
  body_add_par("hello world 2", style = "Normal") %>% 
  print(target = "doc2.docx")


read_docx(path = "doc1.docx") %>%
  body_add_break() %>% 
  body_add_docx(src="doc2.docx") %>%
  print(target = "out.docx") 

enter image description here