繁体   English   中英

使用官员 package 根据幻灯片 position 编号添加幻灯片

[英]Add slides based on slide position numbers using officer package

我正在使用 Office 创建一个 Power Point 演示文稿。 假设我使用基于blank.pptx的 for 循环创建了 10 页幻灯片(名为10_slides_ppt.pptx )。

我的问题是,有没有一种方法可以在 position 编号为c(3, 6, 9)的幻灯片之前添加一张幻灯片?

最终的ppt会是这样的:

1, 2, (新幻灯片 1), 3, 4, 5, (新幻灯片 2), 6, 7, 8, (新幻灯片 3), 9, 10

代码:

library(officer)

current_pres <- read_pptx('10_slides_ppt.pptx') 
# To add new slide 1, but I don't know how to set position of slide
final_pres <- add_slide(current_pres, layout = "Title and Content", master = "Office Theme") %>% 
  ph_with(current_pres, value = "heading", location = ph_location_type(type = "title")) %>% 
  add_slide(current_pres, layout = "Title and Content", master = "Office Theme") %>% 
  ph_with(current_pres, value = "heading", location = ph_location_type(type = "title")) %>% 
  add_slide(current_pres, layout = "Title and Content", master = "Office Theme") %>% 
  ph_with(current_pres, value = "heading", location = ph_location_type(type = "title"))

一个选项是move_slide ,它允许像这样移动幻灯片。 由于在 pptx 的末尾添加了一张新幻灯片,我将轻微的从末尾( index = length(.) )移动到所需的 position。

注意:随着索引在添加和移动幻灯片时发生变化,我添加并反向移动幻灯片,即从 9 到 3:

library(officer)
library(magrittr)

# Make example pptx
current_pres <- read_pptx()
for (i in seq(10)) {
  current_pres<- add_slide(current_pres, layout = "Two Content", master = "Office Theme")  
}

# To add new slide 1, but I don't know how to set position of slide
final_pres <- add_slide(current_pres, layout = "Title and Content", master = "Office Theme") %>% 
  ph_with(value = "heading", location = ph_location_type(type = "title")) %>%
  move_slide(index = length(.), to = 9) %>% 
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with(value = "heading", location = ph_location_type(type = "title")) %>% 
  move_slide(index = length(.), to = 6) %>% 
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with(value = "heading", location = ph_location_type(type = "title")) %>% 
  move_slide(index = length(.), to = 3)

fn <- tempfile(fileext = ".pptx")
print(final_pres, fn)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM