Search code examples
rofficer

officer placeholder text is not saved to output file


I am trying to produce some powerpoint reports with OfficeR, but am having trouble saving the results to a file.

I am able to process the data and fill the placeholders. The content that is intended for the placeholders is shown when I use slide_summary(), but it is not visible in the output file.

My code looks like this:

# read template
pptx <- read_pptx("sample_pptx.pptx")
# open slide 5
slide <- on_slide(pptx, 5)
# add text to pre-existing placeholder
ph_add_text(slide, str = "sample_text", ph_label = "sample_ph")
# check content of slide
slide_summary(slide)
# save presentation
print(pptx, target = "outfile.pptx")

Output:

> slide_summary(slide)
  type id                             ph_label offx offy cx cy        text
1  body  2                            sample_ph   NA   NA NA NA sample_text

> print(pptx, target = "outfile.pptx")
[1] "C:/Users/mhuber/OfficeR/outfile.pptx"

What ever I do, the text never shows up in my outfile.


Solution

  • Until David figures this out, the following workaround functions just fine and doesn't seem to affect performance much (obviously instead of accessing an existing slide, a new one needs to be created from an existing layout, in which the placeholder needs to be present):

    # read template
    pptx <- read_pptx("sample_pptx.pptx")
    # create new slide
    pptx <- add_slide(pptx, layout="sample_layout", master="sample_master")
    # add text to pre-existing placeholder
    ph_with(pptx, value = "sample_text", location = ph_location_label(ph_label = "sample_ph"))
    # move slide from end to desired position ( in this case 5)
    move_slide(pptx, index=length(pptx), to=5)
    # save presentation
    print(pptx, target = "outfile.pptx")
    

    It gets more tricky if you don't have an existing layout that suits your needs, but this worked for my case and hopefully it does for others as well.