1

The R officer package is very good at letting you insert text into existing placeholders in a powerpoint deck. For instance, this code:

library(tidyverse)
library(officer)

pres <- (read_pptx()
  %>% add_slide(layout = "Title and Content", master = "Office Theme")
  %>% ph_with_text(type="body", str="placeholder")
  %>% ph_add_par(level=2)
  %>% ph_add_text("Foo")
  %>% ph_add_par(level=3)
  %>% ph_add_text("Bar")
  %>% ph_add_par(level=4)
  %>% ph_add_text("Baz")
  %>% print(target="bullet_example1.pptx"))

produces a powerpoint with bullets that look like this. However, it seems to insert text at an arbitrary location, I have to use the ph_empty_at() function like this:

pres <- (read_pptx()
  %>% add_slide(layout = "Title and Content", master = "Office Theme")
  %>% ph_empty_at(left=2, top=2, width=5, height=5)
  %>% ph_add_par(level=1)
  %>% ph_add_text("Placeholder")
  %>% ph_add_par(level=2)
  %>% ph_add_text("Foo")
  %>% ph_add_par(level=3)
  %>% ph_add_text("Bar")
  %>% ph_add_par(level=4)
  %>% ph_add_text("Baz")
  %>% print(target="bullet_example2.pptx"))

However, this results in text that looks very different and doesn't respect the levels argument. It seems the text is not inheriting the style from the slide.

I'm asking because I need to use a pre-specified PPT template. I can do this with an existing placeholder, and I get the desired output. How can I insert formatted text like this at an arbitrary location on the slide?

bescoto
  • 11
  • 2
  • If not a bug, I will add a type argument so that the new shape can inherit from the `type` shape... – David Gohel Jul 15 '17 at 07:47
  • I don't think it's a bug---the function is to add an empty placeholder, and looking at the code it seems that's what's happening. But additional functionality to inherit from an existing placeholder would be great! PS thanks for officer package! – bescoto Jul 15 '17 at 23:35
  • David added the `template_type` and `template_index` arguments to the `ph_empty_at` function of the officer package. See https://github.com/davidgohel/officer/issues/47. So above you could add `template_type="body"` and get the result I wanted. – bescoto Nov 22 '17 at 21:41

1 Answers1

0

Hi Using layout = "Blank" seems to work

pres <- (read_pptx()
     %>% add_slide(layout = "Blank", master = "Office Theme")
     %>% ph_empty_at(left=0, top=0, width=5, height=5)
     %>% ph_add_par(level=1)
     %>% ph_add_text("Placeholder")
     %>% ph_add_par(level=2)
     %>% ph_add_text("Foo")
     %>% ph_add_par(level=3)
     %>% ph_add_text("Bar")
     %>% ph_add_par(level=4)
     %>% ph_add_text("Baz")
     %>% ph_empty_at(left=5, top=5, width=5, height=5)
     %>% ph_add_par(level=1, id_chr = 3)
     %>% ph_add_text("Placeholder", id_chr = 3)
     %>% ph_add_par(level=2, id_chr = 3)
     %>% ph_add_text("Foo", id_chr = 3)
     %>% ph_add_par(level=3, id_chr = 3)
     %>% ph_add_text("Bar", id_chr = 3)
     %>% ph_add_par(level=4, id_chr = 3)
     %>% ph_add_text("Baz", id_chr = 3)
     %>% print(target="bullet_example2.pptx")
     )
  • I accidentally down-voted your answer @danilo-mercurio, sorry – David Gohel Sep 17 '17 at 14:00
  • This is a good workaround in this case, but means you can't specify the layout. The officer package now has an explicit `template_type` argument to `ph_empty_at`, so you choose whatever layout you want, and still have your placeholder inherit the desired type. – bescoto Nov 22 '17 at 21:44
  • Thanks for the answer. It was really useful. However, I could not understand much the use of `template_index`. When should we use that? If I have added a slide with layout as "Blank" and then I am adding placeholders in the slide using `ph_empty_at`, how can I use `template_index` in this case? – Prince Aug 02 '18 at 06:25