1

I followed the steps in this answer to combine two documents Writing word documents with the officer package: How to combine several rdocx objects?

But one document is a frontpage and the other document has headers. The problem is that when I join them, the front page also has headers.

How can I join or merge two documents while keeping separated the header from the frontpage?

#creating the front page. 
#summarized version since it will have added features
my_doc_frontpage <- read_docx()
    
my_doc_frontpage <- my_doc_frontpage %>%
      body_add_img(src = "logo.png", width = 1, height = 0.8, style = "centered") 
print(my_doc_frontpage,"frontpage.docx")

#reading the file containing some headers. 
#summarized version since it will have added features
my_doc <- read_docx("base.docx")

my_doc <-   body_add_docx(my_doc,"frontpage.docx")
    
print(my_doc,"combined.docx")

but the combined.docx file has the frontpage with the header modified

stefan
  • 90,330
  • 6
  • 25
  • 51
Mikael
  • 113
  • 1
  • 8
  • I'm facing a similar issue. Watching this,as I think it might be a bug. – Sven Sep 14 '20 at 14:22
  • @Sven I think I found a solution. I defined a template with two pages, but different sections ("different from previous"). In the header of the front page wrote "frontcode", for example, and in the next one "heatherline1". And then use headers_replace_text_at_bkm to clean the frontcode and set the desired heather at heatherline1. – Mikael Oct 06 '20 at 18:21
  • sorry, I meant headers_replace_all_text instead of text at bkm, that last one didn´t worked – Mikael Oct 06 '20 at 21:17

1 Answers1

2

So, after some time I found a way to solve this.

  1. Define a template.docx, with two page pages (each one a different section)
  2. On the header of each page write a keyword "front1" and "page1", for example
  3. In your code use "headers_replace_all_text"
my_doc <- read_docx("template.docx")
my_doc <- my_doc %>% headers_replace_all_text("front1"," ") #blank since it's frontpage
my_doc <- my_doc %>% headers_replace_all_text("page1","Document") #new header

And that's it

Mikael
  • 113
  • 1
  • 8