Reputation: 175
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.
Upvotes: 5
Views: 10519
Reputation: 1
The below code worked for me in Python -->
import win32com.client
import os
path1="C:\Users\SagarMRao\Downloads\genppt.pptx"
path2="C:\Users\SagarMRao\Downloads\processid_1.pptx"
path3="C:\Users\SagarMRao\Downloads\processid_2.pptx"
lst=[path1,path2,path3]
output_path="C:\Users\SagarMRao\Downloadstest_combined.pptx"
def merge_presentations(presentations, path):
ppt_instance = win32com.client.Dispatch('PowerPoint.Application')
prs = ppt_instance.Presentations.open(os.path.abspath(presentations[0]), True, False, False)
for i in range(1, len(presentations)):
prs.Slides.InsertFromFile(os.path.abspath(presentations[i]), prs.Slides.Count)
prs.SaveAs(os.path.abspath(path))
prs.Close()
merge_presentations(lst,output_path)
Upvotes: 0
Reputation: 23443
I made this solution for two specific pptx
from pptx import Presentation
import os
prs1 = Presentation("Esempio.pptx")
prs2 = Presentation("Example.pptx")
for slide in prs2.slides:
sl = prs1.slides.add_slide(prs1.slide_layouts[1])
sl.shapes.title.text = slide.shapes.title.text
sl.placeholders[1].text = slide.placeholders[1].text
prs1.save("Esempio_Example.pptx")
And this for all the pptx in a folder
from pptx import Presentation
import os
import glob
pres = glob.glob("*.pptx")
prs1 = Presentation(pres[0])
# prs2 = Presentation("Example.pptx")
for presentation in pres[1:]:
pres = Presentation(presentation)
for slide in pres.slides:
sl = prs1.slides.add_slide(prs1.slide_layouts[1])
sl.shapes.title.text = slide.shapes.title.text
try:
sl.placeholders[1].text = slide.placeholders[1].text
except:
sl.placeholders[1].image = slide.placeholders[1].image
prs1.save("all_together.pptx")
Upvotes: 1
Reputation: 939
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))
Upvotes: 0
Reputation: 41
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.
Upvotes: 3