使用 officer 包根据幻灯片位置编号添加幻灯片

Add slides based on slide position numbers using officer package

我正在使用 Officer 创建 Power Point 演示文稿。假设我使用 blank.pptx.

中的 for 循环创建了 10 页幻灯片(名为 10_slides_ppt.pptx

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

最后的ppt是这样的:

1, 2, (new slide 1), 3, 4, 5, (new slide 2), 6, 7, 8, (new slide 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(.)) 移动到所需位置。

注意:随着索引在添加和移动幻灯片时发生变化,我以相反的方向添加和移动幻灯片,即从 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)