1

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?

ah bon
  • 9,293
  • 12
  • 65
  • 148
J.Sabree
  • 2,280
  • 19
  • 48
  • @MrFlick, though I can't share the templates/data to reproduce the error directly, I included my exact code in the most recent edit. – J.Sabree Aug 11 '20 at 20:50

1 Answers1

3

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")
MrFlick
  • 195,160
  • 17
  • 277
  • 295