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 !?
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")
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")
Related
I have an rdocx and I want to manipulate something in the xml code. That's my document:
library(officer)
doc <- read_docx() %>%
body_add_par("centered text", style = "centered") %>%
slip_in_seqfield("STYLEREF 1 \\s") %>%
slip_in_text("\u2011") %>%
slip_in_seqfield(sprintf("SEQ %s \\* ARABIC \\s 1", "Table")) %>%
slip_in_text(str_c(": ", "My Caption")) %>%
body_bookmark("my_bookmark")
With doc$doc_obj$get() I can get the xml code with classes xml_document and xml_node. Now I want to replace some code, in detail I want the part with w:bookmarkEnd to appear later so the bookmarked part gets bigger. How can I achieve this? If I could achieve this with str_replace it would be awesome.
You can use run_bookmark() as in the following example (the manual does not state that lists are supported, I'll add that info soon):
library(officer)
bkm <- run_bookmark(
bkm = "test",
list(
run_word_field(field = "SEQ tab \\* ARABIC \\s"),
ftext(" Table", prop = fp_text_lite(color = "red"))
)
)
doc <- read_docx()
doc <- body_add_fpar(
x = doc,
value = fpar(bkm)
)
# how to make a reference to the bkm
doc <- body_add_fpar(
x = doc,
value = fpar(run_reference("test"))
)
print(doc, "zz.docx")
Using officer in R, I've used ph_slidelink() to hyperlink a text box to another slide in the presentation, and I've used compose() and hyperlink_text() to hyperlink a cell within a flextable. My question is: is there a way to combine these, and to hyperlink to another slide in the presentation within the cell of a flextable?
Here's a very simple example of code I'd like to transform:
library(officer)
library(flextable)
library(magrittr)
ft <- data.frame(slide_number = seq(3)) %>%
flextable() %>%
width(width = 3)
doc <- read_pptx() %>%
add_slide() %>%
ph_with("Table of Contents", location = ph_location_label("Title 1")) %>%
ph_with(ft, location = ph_location_label("Content Placeholder 2"))
for (i in seq(3)) {
doc <- doc %>%
add_slide() %>%
ph_with(paste("Slide", i), location = ph_location_label("Title 1"))
}
print(doc, target = "~/Desktop/officer_example.pptx" )
...and in this case I'd like the 1/2/3 in the table of contents (here) to link to slides 1/2/3.
Is this possible?
When exporting a flextable to *.pptx using 'officer' if the flextable has a footer it seems to corrupt the power point and it needs to be repaired.
I've tried using the other footer functions. I also tried adding a header to see if that similar code throws the error. Adding headers seem fine.
I also tried going back to officer v. 0.3.3.
Officer is v. 0.3.4
flextable is v. 0.5.4
R is 3.6.0 "Planting of a Tree"
library(officer)
library(flextable)
library(dplyr)
pdoc <- read_pptx()
footer <- "foot"
header <- "head"
myft <- iris %>% head() %>% flextable()
myft <- myft %>% add_header_lines( values = header)
pdoc <- pdoc %>%
add_slide(layout = "Title and Content", master = "Office Theme") %>%
ph_with_flextable(myft , type = "body")
print(pdoc, "test1passes.pptx")
myft <- myft %>% add_footer_lines( values = footer)
pdoc <- pdoc %>%
add_slide(layout = "Title and Content", master = "Office Theme") %>%
ph_with_flextable(myft , type = "body")
print(pdoc, "test2fails.pptx")
I can open the "test1passes.pptx" file with no problem.
When I open the "test2fails.pptx" I get an error from pptx saying the file is corrupted when I expected that it should open with no problem.
I'm trying to add line breaks using body_replace_all_text or body_add_par but am having no joy. Using \r\n shows correctly in OSX TextEdit, but not in Word.
An example:
library(officer)
library(tidyverse)
read_docx() %>%
body_add_par("Oneline\r\n\r\nAnother line") %>%
print(target = "example.docx")
is there a right way to do this?
You will have to call body_add_par each time you want to add a paragraph (a paragraph of text ends with a new line):
library(officer)
library(tidyverse)
read_docx() %>%
body_add_par("Oneline") %>%
body_add_par("Another line") %>%
print(target = "example.docx")
An alternative way that I found was to modify in Word.
library(officer)
library(tidyverse)
read_docx() %>%
body_add_par("Oneline(LineBreak)Another line") %>%
print(target = "example.docx")
Then in Word, press Ctrl + H and change all "(LineBreak)" to "^p".
Not a fancy idea but it worked for me as a band aid solution.
Here is a simple workaround with splitting the string into a vector and writing each line separately. Unfortunately it is very slow.
library(officer)
library(tidyverse)
doc <- read_docx()
text <- "Oneline\r\n\r\nAnother line" %>% strsplit('\r\n') %>% unlist()
for (t in text){
body_add_par(doc, t)
}
print(doc, target = "example.docx")
I am using officer for R to create a a Word (docx) document. Here is the code:
library(officer)
library(magrittr)
my_docx = read_docx() %>%
# cursor_begin() %<>%
body_add_par("Hello here is a test sentence to see if there is a blank line above it.", style = "Normal") %>%
print(target = "test.docx")
It creates a docx document that has a blank line at the top above the sentence. It isn't spacing before the font style that is set in the Style of the font. I have uncommented cursor_begin() but the blank line remains. How can I get rid of this?
Any help is appreciated!
To delete a paragraph, you need to use function body_remove(). See documentation here: https://davidgohel.github.io/officer/articles/word.html#remove-content
library(officer)
library(magrittr)
my_docx <- read_docx() %>%
cursor_begin() %>% body_remove() %>%
body_add_par("Hello here is a test sentence to see if there is a blank line above it.",
style = "Normal") %>%
print(target = "test.docx")