官员占位符文本未保存到输出文件

时间:2019-07-12 09:34:55

标签: r officer

我正在尝试使用OfficeR生成一些PowerPoint报告,但是在将结果保存到文件时遇到麻烦。

我能够处理数据并填充占位符。当我使用slide_summary()时会显示用于占位符的内容,但在输出文件中看不到。

我的代码如下:

# 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")

输出:

> 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"

无论我做什么,文本都不会出现在我的文件外。

1 个答案:

答案 0 :(得分:0)

直到David弄清楚这一点,以下变通办法的功能都很好,并且似乎不会对性能产生太大影响(显然,代替访问现有的幻灯片,需要从现有的布局中创建一个新的幻灯片,在该布局中需要占位符)出现):

# 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")

如果您没有适合您需要的现有布局,这将变得更加棘手,但这对于我的情况很有用,希望对其他人也适用。