0

I'm part of a project team that created PPTX presentations to present to clients. After creating all of the files, we need to add additional slides to each presentation. All of the new slides will be the same across the each presentation.

What is the best way to accomplish this programmatically?

I don't want to use VBA because (as far as I understand) I would have to open each presentation to run the script.

I've tried using the python-pptx library. But the documentation states:

"Copying a slide from one presentation to another turns out to be pretty hard to get right in the general case, so that probably won’t come until more of the backlog is burned down."

I was hoping something like the following would work -

from pptx import Presentation

main = Presentation('Universal.pptx')
abc = Presentation('Test1.pptx')

main_slides = main.slides.get(1)
abc_slides = abc.slides.get(1)

full = main.slides.add_slide(abc_slides[1])

full.save('Full.pptx')

Has anyone had success do anything like that?

Brad Lide
  • 3
  • 1
  • 2
  • "I don't want to use VBA because (as far as I understand) I would have to open each presentation to run the script." Each presentation would need to be opened in PPT in order for VBA to do any good, but the VBA in one presentation or add-in could automate the work of opening each presentation in a list or in a folder and doing whatever's needed, then saving and closing it before moving on to the next presentation. – Steve Rindsberg Sep 14 '17 at 02:20
  • That's a really interesting idea. I'll have to take a look into that. Any way you could point me in the direction of a good source for inexperienced-friendly documentation? Thanks again! – Brad Lide Sep 15 '17 at 04:42
  • There's a section on PowerPoint programming in the PPT FAQ site that I maintain here: http://www.pptfaq.com/index.html#name_PROGRAMMING_POWERPOINT Lots of examples and links to other sites on the subject. This entry might be useful for your project: http://www.pptfaq.com/FAQ00536_Batch-_Do_something_to_every_file_in_a_folder.htm – Steve Rindsberg Sep 15 '17 at 15:09
  • Better late than never, thanks so much for your guidance. We ended up grabbing a team to workhorse it manually, but I learned a lot using those resources and have them in my toolbelt for the future when a project may not be so time sensitive. Thanks again. – Brad Lide Oct 04 '17 at 16:47

3 Answers3

1

I was able to achieve this by using python and win32com.client. However, this doesn't work quietly. What I mean is that it launches Microsoft PowerPoint and opens input files one by one, then copies all slides from an input file and pastes them to an output file in a loop.

import win32com.client
from os import walk

def mergePresentations(inputFileNames, outputFileName):

    Application = win32com.client.Dispatch("PowerPoint.Application")
    outputPresentation = Application.Presentations.Add() 
    outputPresentation.SaveAs(outputFileName)

    for file in inputFileNames:    
        currentPresentation = Application.Presentations.Open(file)
        currentPresentation.Slides.Range(range(1, currentPresentation.Slides.Count+1)).copy()
        Application.Presentations(outputFileName).Windows(1).Activate()    
        outputPresentation.Application.CommandBars.ExecuteMso("PasteSourceFormatting")    
        currentPresentation.Close()

    outputPresentation.save()
    outputPresentation.close()
    Application.Quit()

# Example; let's say you have a folder of presentations that need to be merged 
#           to new file named "allSildesMerged.pptx" in the same folder

path,_,files = next(walk('C:\\Users\\..\\..\\myFolder'))
outputFileName = path + '\\' + 'allSildesMerged.pptx'

inputFiles = []
for file in files:
    inputFiles.append(path + '\\' + file)

mergePresentations(inputFiles, outputFileName)

Zeynel
  • 41
  • 6
  • There must be something more to this than the code above. When executing, I'm getting pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft PowerPoint', 'Presentation.SaveAs : An error occurred while PowerPoint was saving the file.', '', 0, -2147467259), None) at the for SaveAs part so things haven't even really started here yet. – Mikko Jaakkola Apr 29 '21 at 18:44
  • I am not sure why would you get that error. Are you sure that you have permission to save? – Zeynel May 09 '21 at 04:59
  • Definitely, permissions were part of it as I need to run the script in Admin-terminal window. Also, changing into active window didn't work for as in code so I needed to change that. My complete function below so I changed... Application.Presentations(outputFileName).Windows(1).Activate() -> Application.Presentations(outputFileName).Windows(1).Activate() – Mikko Jaakkola May 10 '21 at 17:51
  • @MikkoJaakkola, can you tell me what you changed one more time? Because both lines look identical. – Zeynel May 11 '21 at 18:32
  • 2
    They look the same as they are :-) I swapped Application.Presentations(outputFileName).Windows(1).Activate() with outputPresentation.Windows(1).Activate(). Somehow it didn't find the right presentation with the filename which is weird as it used the filename that was used to save the presentation earlier. – Mikko Jaakkola May 12 '21 at 19:27
0

The GroupDocs.Merger REST API is also another option to merge multiple PowerPoint presentations into a single document. It is paid API but provides 150 monthly free API calls.

Currently, it supports working with cloud providers: Amazon S3, DropBox, Google Drive Storage, Google Cloud Storage, Windows Azure Storage, FTP Storage along with GroupDocs internal Cloud Storage. However, in near future, it has a plan to support merge files from the request body(stream).

P.S: I'm developer evangelist at GroupDocs.

# For complete examples and data files, please go to https://github.com/groupdocs-merger-cloud/groupdocs-merger-cloud-python-samples
# Get Client ID and Client Secret from https://dashboard.groupdocs.cloud
client_id = "XXXX-XXXX-XXXX-XXXX" 
client_secret = "XXXXXXXXXXXXXXXX"
  
documentApi = groupdocs_merger_cloud.DocumentApi.from_keys(client_id, client_secret)
 
item1 = groupdocs_merger_cloud.JoinItem()
item1.file_info = groupdocs_merger_cloud.FileInfo("four-slides.pptx")
item2 = groupdocs_merger_cloud.JoinItem()
item2.file_info = groupdocs_merger_cloud.FileInfo("one-slide.docx")
 
options = groupdocs_merger_cloud.JoinOptions()
options.join_items = [item1, item2]
options.output_path = "Output/joined.pptx"
 
result = documentApi.join(groupdocs_merger_cloud.JoinRequest(options))
Tilal Ahmad
  • 940
  • 5
  • 9
-2

A free tool called "powerpoint join" can help you.

Statham
  • 4,000
  • 2
  • 32
  • 45