简体   繁体   中英

Customize text in the ph_with function from the officer package in R

I am trying to automate my PowerPoint reports using the officer package; however, I cannot find out how to customize the text (font size, color, font).

To add in text, I'm using the ph_with() function. I tried to customize it using officer's ftext() function, but it doesn't work. With this example

library(officer)

dataframe <- data.frame(column="Sample")
ppt_temp <- read_pptx()
ppt_temp <- add_slide(ppt_temp)

properties <- fp_text(color = "red", font.size = 28, bold = TRUE)

slide_title <- ftext(paste0("These data cover ", dataframe$column[1]), properties)

slide <- ph_with(x = ppt_temp, value = slide_title, 
   location = ph_location_type(type = "title"))

I get the following error

Error in UseMethod("ph_with", value) :
  no applicable method for "ph_with" applied to an object of class "c('ftext', 'cot', 'run')"

If I don't try to set the text properties, this will work

slide <- ph_with(x = ppt_temp, value = paste0("These data cover ", dataframe$column[1]), 
    location = ph_location_type(type = "title))

Any suggestions?

It seems ph_with expects the value to be in a paragraph wrapper. Wrap your text object in fpar() . Try

slide <- ph_with(x = ppt_temp, value = fpar(slide_title), 
    location = ph_location_type(type = "title"))

So a fully working example is

library(officer)

dataframe <- data.frame(column="Sample")
ppt_temp <- read_pptx()
ppt_temp <- add_slide(ppt_temp)

properties <- fp_text(color = "red", font.size = 28, bold = TRUE)
slide_title <- ftext(paste0("These data cover ", dataframe$column[1]), properties)
slide <- ph_with(x = ppt_temp, value = fpar(slide_title),
                 location = ph_location_type(type = "title"))

print(ppt_temp, target="temp.pptx")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM