-1

Why doesn't work body_add_docx method in package "officer"? Where did I make mistake?

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 !?
Sergey Nosov
  • 350
  • 3
  • 10

2 Answers2

2

Code below works only for Windows, MS Word and only without page break.

For Linux, LibreOffice, google document it doesn't work.

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") 
Sergey Nosov
  • 350
  • 3
  • 10
0

The function body_add_docx is using a MS Word feature. When the document is edited, the content of the file is copied in the main document, but that only happens when the document is edited by Word. LibreOffice and gdoc probably don't have this feature implemented (at least I am not aware of them).

The script below is producing the expected document only when edited with 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

David Gohel
  • 9,180
  • 2
  • 16
  • 34
  • 1
    Given that this doesn't work in Linux or LibreOffice, is there another way to assemble large documents as done [here](https://ardata-fr.github.io/officeverse/officer-for-word.html#external-documents)? – S. Robinson Jan 27 '22 at 18:53
  • bookdown probably – Ferroao Oct 21 '22 at 20:51