【问题标题】:Add slides based on slide position numbers using officer package使用官员包根据幻灯片位置编号添加幻灯片
【发布时间】:2022-01-24 09:54:28
【问题描述】:

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

我的问题是,有没有办法可以在位置编号为 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"))

【问题讨论】:

    标签: r officer


    【解决方案1】:

    一个选项是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)
    

    【讨论】:

    • 多么棒的答案!感谢您帮助我解决此问题。我想如果包的贡献者可以开发基于当前ppt索引添加幻灯片的功能,它会更容易使用。
    猜你喜欢
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    • 2015-04-21
    • 2023-01-10
    相关资源
    最近更新 更多