Home > Back-end >  Add slides based on slide position numbers using officer package
Add slides based on slide position numbers using officer package

Time:12-24

I am creating a Power Point presentation using Officer. Let's say I have created a 10 pages slides (named 10_slides_ppt.pptx) using for loop from a blank.pptx.

My question is, is there a way that I can add one slide before slides with position number of c(3, 6, 9)?

The final ppt will like this:

1, 2, (new slide 1), 3, 4, 5, (new slide 2), 6, 7, 8, (new slide 3), 9, 10

Code:

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

CodePudding user response:

One option would be move_slide which allows to move a slide like so. As a new slide is added at the end of the pptx I move the slight from the end (index = length(.)) to the desired position.

Note: As the indices change while adding and moving slides I add and move the slides in reverse direction, i.e. from 9 to 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)
  • Related