【问题标题】:officer placeholder text is not saved to output file军官占位符文本未保存到输出文件
【发布时间】:2019-07-12 21:42:16
【问题描述】:

我正在尝试使用 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"

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

【问题讨论】:

  • 我无法运行代码,因为缺少 sample_pptx.pptx。你能让代码可重现吗?
  • 嗨大卫,很高兴收到你这么快的消息! sample_pptx.pptx 只是一个空文件,包含 5 张幻灯片和第 5 张幻灯片的占位符。You can download it here
  • 那里可能有错误...我需要更多时间来理解。你不能用 ph_with 代替现有的空幻灯片吗?在您的模板中,您的幻灯片都是空的,ph_add_text 仅用于将文本添加到现有形状/ph 中。
  • 这只是一个小例子,不幸的是,我真正的 powerpoint 输入文件在幻灯片 5 之前和之后已经有内容。我可能会尝试使用 move_slide 解决这个问题,但我当然更愿意让我最初的想法发挥作用。
  • 当然,我明白了。我将很快改进 doc et 希望有时间解决这个问题。很抱歉给您带来不便

标签: r officer


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-21
    • 1970-01-01
    • 2023-02-15
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多