13

I have been using officer package to create the respective PowerPoint decks, however at this moment, i would like to merge/ bind them all as one slide deck and was not able to figure out. Can someone guide me if there any package that helps to merge multiple PowerPoint decks into one.

Dinesh
  • 391
  • 2
  • 9
  • 1
    I have the same quesiton ! I would love to know the answer to this. – clairekelley Jun 07 '20 at 12:37
  • 2
    Is this useful: https://www.online-tech-tips.com/free-software-downloads/merge-powerpoint-ppt-files/ – mkareshky Aug 19 '20 at 04:06
  • @J.sabree do you have to use the officer package or can you use Rstudio's Rmarkdown and render to PPTX with a custom PPTX template there? – Mike Aug 24 '20 at 13:09

2 Answers2

2

I believe currently there are no functions or packages that do this in R, so I'll suggest you a few possible solutions that come to mind.

1: I believe you could use read_pptx() to read, say, a deck1 and a deck2 files. Then, loop through the slide indexes of deck2, and use those values to add_slide() into deck1. I think there's a function in officer called pptx_summary(), which converts a pptx R object into a tibble, but I'm not sure you could convert a tibble back to a pptx R object.

2: You could convert pptx files into pdf files, and use pdftools to join them.

eduardokapp
  • 1,612
  • 1
  • 6
  • 28
0

When creating PowerPoint slides automatically via R (for example by using the PowerPoint export of R markdown), merging them with pre-manufactured fixed slides (for example explanations with elaborate visuals) may likely become necessary. As there seems not single-line solution so far, here's an incomplete answer to a 3-year-old question.

A look into the sources of OfficeR shows that the package works with a data structure and a temporary folder in the background that contains the XML files that are zipped in the XLSX file.

Copying slides, therefore, requires both: To update the structure, and to copy XML files and other ressources, eventually. Here is a very rough draft of how merging two PowerPoint files can work, based on the OfficeR classes.

merge_pptx = function(a, b, filename) {
    # go through the slides of b
    for (index in 1:length(source$slide$get_metadata())) {
    
        # We need a new filename in the target's slide directory
        new_slidename <- target$slide$get_new_slidename()
        xml_file <- file.path(target$package_dir, "ppt/slides", new_slidename)
    
        # Copy XML from source to new filename
        orgFilename = source$slide$get_metadata()[index, "filename"]
        newFilepath = paste(target$package_dir, newFilename, sep="/")
        file.copy(orgFilename, xml_file)
    
        # Not sure yet, what exactly this does
        slide_info <- target$slideLayouts$get_metadata()[1,]  # Use first best layout at the moment
        layout_obj <- target$slideLayouts$collection_get(slide_info$filename)
        layout_obj$write_template(xml_file)
    
        # update presentation elements
        target$presentation$add_slide(target = file.path("slides", new_slidename))
        target$content_type$add_slide(partname = file.path("/ppt/slides", new_slidename))
     
        # Add the slide to the collection   
        target$slide$add_slide(xml_file, target$slideLayouts$get_xfrm_data())
    
        target$cursor <- target$slide$length()
    }

    print(target, target=filename)
}

source = read_pptx("One.pptx")
target = read_pptx("Two.pptx")
merge_pptx(source, target, "Combined.pptx")

Please note, that this is a draft only. It does not yet respect the different layouts for slides, not even speaking of different masters. Embedded files (images) are not yet copied.

The bulk of this function is inspired by the add_slide() function in the dir_slide class, see https://github.com/davidgohel/officer/blob/master/R/ppt_class_dir_collection.R

BurninLeo
  • 4,240
  • 4
  • 39
  • 56