3
votes

I am trying to combine powerpoint slides and I am not sure how to do it. I found a python moduel pptx that looked promising. I tried copying the slide contents from 2 powerpoints to one new power point. But I ran into many issues like how to grab the existing slides layout or shape (all shapes including pictures, auto shapes, and more) height, width, position. I looked at the python-pptx example on Python-pptx: copy slide. I tried doing something similar but that isn't working.

Here is my code:

from pptx import Presentation

prs1 = Presentation("C:/Users/number/Documents/Test1.pptx")
prs2 = Presentation("C:/Users/number/Documents/Test2.pptx")

slidelst = []
for layout in prs2.slide_layouts:
    slidelst.append(prs1.slides.add_slide(layout))

index = 0
for slide in slidelst:
    for shape in prs2.slides[prs2.slides.index(slide)].shapes:
        slide.shapes._spTree.insert_element_before(shape.element, 'p:extLst')
    index+=1

prs1.save("C:/Users/I505168/Documents/newpresentation.pptx")

I get the error:

Traceback (most recent call last):
  File "C:\Users\I505168\Desktop\testpptx.py", line 12, in <module>
    for shape in prs2.slides[prs2.slides.index(slide)].shapes:
  File "C:\Users\I505168\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pptx\slide.py", line 315, in index
    raise ValueError("%s is not in slide collection" % slide)
ValueError: <pptx.slide.Slide object at 0x03B53A50> is not in slide collection

The expected result is two separate slides merge into one slide.

2
Hi! I would love to know if you ended up finding a programmatic way to do this.Kyle McComb
Copying slides from another presentation is currently not supported. python-pptx.readthedocs.io/en/latest/user/…MrU
@kingofjong Did you solve the problem. If yes, can you share the solutionNaren Babu R

2 Answers

1
votes

I was able to solve this problem by using python and win32com.client and maybe it is not what you need because it launches Microsoft Powerpoint and copies and pastes the slides from input files to an output file. Then closes the PowerPoint application. If this method works for you here is the link to my answer to another question.

0
votes

Another solution to combine PowerPoint Presentations in python is GroupDocs.Merger Cloud SDK for Python. It is a REST API solution without the dependency of any third party app/tool. Currently, it combines the files from Cloud storage(GroupDocs internal storage, Amazon S3, DropBox, Google Drive Storage, Google Cloud Storage, Windows Azure Storage and FTP Storage.). However, we have a plan to combine files from the request body(stream) in near future.

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

# Import module
import groupdocs_merger_cloud
from shutil import copyfile

# Get your client Id and Secret at https://dashboard.groupdocs.cloud (free registration is required).
clientid = "xxxxx-xxxx-xxxx-xxxx-xxxxxxxx"
clientsecret = "xxxxxxxxxxxxxxxxxxxxxxxxx"

# Create instance of the API
documentApi = groupdocs_merger_cloud.DocumentApi.from_keys(clientid, clientsecret)
file_api = groupdocs_merger_cloud.FileApi.from_keys(clientid, clientsecret)

try:

    #upload source files to default storage
    filename1 = 'C:/Temp/test.pptx'
    remote_name1 = 'slides/test.pptx'
    filename2 = 'C:/Temp/three-slides.pptx'
    remote_name2 = 'slides/three-slides.pptx'

    output_name= 'slides/joined.pptx'


    request_upload1 = groupdocs_merger_cloud.UploadFileRequest(remote_name1,filename1)
    response_upload1 = file_api.upload_file(request_upload1)
    request_upload2 = groupdocs_merger_cloud.UploadFileRequest(remote_name2,filename2)
    response_upload2 = file_api.upload_file(request_upload2)
 
    item1 = groupdocs_merger_cloud.JoinItem()
    item1.file_info = groupdocs_merger_cloud.FileInfo(remote_name1)
    item2 = groupdocs_merger_cloud.JoinItem()
    item2.file_info = groupdocs_merger_cloud.FileInfo(remote_name2)
 
    options = groupdocs_merger_cloud.JoinOptions()
    options.join_items = [item1, item2]
    options.output_path = output_name
 
    result = documentApi.join(groupdocs_merger_cloud.JoinRequest(options))

        
    #Download Document from default Storage        
    request_download = groupdocs_merger_cloud.DownloadFileRequest(output_name)
    response_download = file_api.download_file(request_download)
       
    copyfile(response_download, 'C:/Temp/joined.pptx')
    print("Result {}".format(response_download))
        
except groupdocs_merger_cloud.ApiException as e:
        print("Exception when converting document: {0}".format(e.message))