4

I want to add several placeholders to a Power Point slide, in different (fixed) positions, using a predefined template of my company. I use that template only to get the font style, the header and footer of the slides,but not for the content of each slide. For this, I can add text and images wherever I want. Then I need to add, in each of these placeholders, some text paragraphs. I can do this, for example, with the code below. But the bad thing is that if I want to add a paragraph to the first placeholder, I have to specify the id_chr of the placeholder (in this example are "2" and "3") which is known only after I add the empty placeholders in the presentation, at runtime.

Is there a way to add an empty placeholder specifying also an ID that can be used later to add content to it using that ID instead of the id_chr that is known only at runtime? Maybe using also ph_label?

require(magrittr)
require(officer)

TARGET_FILE = "ph_add_fpar.pptx"
setwd(file.path("E:", "Work", "Reporting"))
file.remove(TARGET_FILE)

MASTER_LAYOUT = "Blank"
PICTURE_AND_TEXT_PAGE_LAYOUT = "Internal Slide - Picture and Text"

bold_face <- shortcuts$fp_bold(font.size = 30)
bold_redface <- update(bold_face, color = "red")

fpar_1 <- fpar(ftext("Hello ", prop = bold_face),
              ftext("World", prop = bold_redface ),
              ftext(", \r\nhow are you?", prop = bold_face ) )

fpar_2 <- fpar(ftext("Hello ", prop = bold_face),
               ftext("World", prop = bold_redface ),
               ftext(", \r\nhow are you again?", prop = bold_face ) )

doc <- read_pptx(path = file.path(getwd(), "Template.pptx")) %>%
  add_slide(layout = PICTURE_AND_TEXT_PAGE_LAYOUT, master = MASTER_LAYOUT) %>%
  ph_empty_at(left = 1,top = 1,width = 3,height = 2,template_type = "body",template_index = 4) %>%
  ph_empty_at(left = 4,top = 1,width = 3,height = 2,template_type = "body",template_index = 4) %>%
  ph_add_fpar(value = fpar_1, type = "body", level = 1, id_chr = "2") %>%
  ph_add_fpar(value = fpar_2, type = "body", level = 1, id_chr = "3")

print(doc, target = TARGET_FILE)

system("cmd.exe", input = TARGET_FILE)

Thank you

David Gohel
  • 9,180
  • 2
  • 16
  • 34

0 Answers0