Combine and Merge PowerPoint PPT/PPTX Files in Python

Combine and Merge PowerPoint PPT/PPTX Files in Python

PowerPoint presentation is a collection of slides where each slide can comprise of text, images, animations, and media etc. Merging PowerPoint presentations by copying and pasting slides one by one into the primary presentation is time consuming process. So, GroupDocs offers python library that automatically merge PowerPoint files in a few seconds. You can easily combine two or more PPTX files into a single PowerPoint file programmatically on the cloud. In this article, we will learn an easy solution about how to combine and merge PowerPoint PPT/PPTX files in Python.

The following topics shall be covered in this article:

Python PowerPoint Merger REST API - Installation

To combine two or more PPTX files, we will be using the Python SDK of GroupDocs.Merger Cloud API. It allows you to combine two or more files into a single document, or split up one source document into multiple output documents. It also enables you to shift, delete, exchange, rotate or change the page orientation either as portrait or landscape for the whole or preferred range of pages. This SDK supports merging and splitting of all popular document formats such as Word, Excel, PowerPoint, Visio, OneNote, PDF, HTML, etc.

You can install GroupDocs.Merger Cloud to your Python application using the following command in the console:

pip install groupdocs_merger_cloud

Please get your Client ID and Secret from the dashboard before following the mentioned steps. Once you have your ID and secret, add in the code as shown below:

# Import Python SDK in your python application from http://api.groupdocs.cloud
import groupdocs_merger_cloud
# Get client_id and client_secret from https://dashboard.groupdocs.cloud after free registration.
client_id = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
client_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Get File API configurations
configuration = groupdocs_merger_cloud.Configuration(client_id, client_secret)
configuration.api_base_url = "https://api.groupdocs.cloud"
storage_name = "MyStorage"

Merge PowerPoint PPTX Files in Python using REST API

You can combine two PowerPoint PPT/PPTX files programmatically on the cloud by following the simple steps mentioned below:

  1. Upload the PPTX files to the cloud
  2. Merge multiple PPTX files using Python
  3. Download the merged PPTX file

Upload the PPTX Files

Firstly, upload the PPTX files to the cloud using the code example given below:

# Upload PowerPoint files to cloud storage
# Create instance of the API
import glob
file_api = groupdocs_merger_cloud.FileApi.from_config(configuration)
storage_api = groupdocs_merger_cloud.StorageApi.from_config(configuration)
# upload sample files
for filename in glob.iglob("H:\\groupdocs-cloud-data\\upload\\*.pptx", recursive=True):
destFile = filename.replace("H:\\groupdocs-cloud-data\\upload", "", 1)
# check if file already exist
fileExistsResponse = storage_api.object_exists(groupdocs_merger_cloud.ObjectExistsRequest(destFile))
if not fileExistsResponse.exists:
# create upload file request
request = groupdocs_merger_cloud.UploadFileRequest(destFile, filename)
# upload file to the cloud
response = file_api.upload_file(request)
print(response.uploaded)

As a result, the uploaded PPTX files will be available in the files section of your dashboard on the cloud.

Merge Multiple PPTX Files using Python

You can easily merge multiple PPTX files into a single file programmatically by following the steps mentioned below:

  • Create an instance of the DocumentApi
  • Create an instance of the JoinItem
  • Provide the input file path for first JoinItem in the FileInfo
  • Create another instance of the JoinItem
  • Provide the input file path for second JoinItem in the FileInfo
  • Add more JoinItems for merging more than two files
  • Create an instance of the JoinOptions
  • Add a comma separated list of created join items
  • Set the output file path
  • Create an instance of the JoinRequest with JoinOptions
  • Finally, combine files by calling the join() method of the DocumentAPI with JoinRequest

The following code snippet shows how to merge multiple PowerPoint files in Python using REST API:

# How to merge PowerPoint PPTX files in Python
# Create necessary API instances
documentApi = groupdocs_merger_cloud.DocumentApi.from_config(configuration)
# Input source file 1
item1 = groupdocs_merger_cloud.JoinItem()
item1.file_info = groupdocs_merger_cloud.FileInfo("python-testing/powerpoint-one.pptx")
# Input source file 2
item2 = groupdocs_merger_cloud.JoinItem()
item2.file_info = groupdocs_merger_cloud.FileInfo("python-testing/powerpoint-two.pptx")
# Define join options
options = groupdocs_merger_cloud.JoinOptions()
options.join_items = [item1, item2]
options.output_path = "python-testing/joined-files.pptx"
# Create join request
request = groupdocs_merger_cloud.JoinRequest(options)
# Merge pptx files
result = documentApi.join(request)
print("Successfully merged PPTX files: " + str(result))

Download the Merged File

The above code sample will save the merged PPTX file on the cloud. You can download it using the following code sample:

# API initialization to download merged file
import shutil
file_api = groupdocs_merger_cloud.FileApi.from_config(configuration)
# Create download file request
request = groupdocs_merger_cloud.DownloadFileRequest("python-testing\\joined-files.pptx", storage_name)
# Download merged file
response = file_api.download_file(request)
# Move the downloaded file to your directory
shutil.move(response, "H:\\groupdocs-cloud-data\\")

Merge Specific Pages of Multiple PPTX Files in Python

You can easily combine specific pages of multiple PowerPoint files into a single document programmatically by following the steps mentioned below:

  • Create an instance of the DocumentApi
  • Create an instance of the JoinItem
  • Provide the input file path for first JoinItem in the FileInfo
  • Define a list of page numbers in a comma separated array
  • Create another instance of the JoinItem
  • Provide the input file path for second JoinItem in the FileInfo
  • Define start page number and end page number
  • Define the page range mode as OddPages
  • Create an instance of the JoinOptions
  • Add a comma separated list of created join items
  • Set the output file path
  • Create an instance of the JoinRequest with JoinOptions
  • Finally, merge pptx by calling the join() method of the DocumentAPI with JoinRequest

The following code snippet shows how to merge specific pages of PPTX files using REST API in Python:

# How to merge specific pages of multiple PPTX files in Python
# Create and initialize api instances
documentApi = groupdocs_merger_cloud.DocumentApi.from_config(configuration)
# Input source file 1
item1 = groupdocs_merger_cloud.JoinItem()
item1.file_info = groupdocs_merger_cloud.FileInfo("python-testing/powerpoint-one.pptx")
# slide numbers of specific slides to merge
item1.pages = [1,2]
# Input source file 2
item2 = groupdocs_merger_cloud.JoinItem()
item2.file_info = groupdocs_merger_cloud.FileInfo("python-testing/powerpoint-two.pptx")
# start slide number
item2.start_page_number = 2
# end slide number
item2.end_page_number = 4
# slides range mode
item2.range_mode = "OddPages"
# Define join options
options = groupdocs_merger_cloud.JoinOptions()
options.join_items = [item1, item2]
options.output_path = "python-testing/joined-slides.pptx"
# Create join request
request = groupdocs_merger_cloud.JoinRequest(options)
# Merge pptx files
result = documentApi.join(request)
print("Successfully merged PPTX slides: " + str(result))

Try Online

How to combine PPTX online? Please try the following free online PPTX merger tool, which is developed using the above API.

Conclusion

In this tutorial, we have learned:

  • how to merge multiple PPTX files on the cloud using python;
  • programmatically upload and download the merged file;
  • how to combine specific pages of multiple PPTX files into single file in Python;

Additionally, you can learn more about GroupDocs.Merger Cloud API using the documentation. We also provide an API Reference section that lets you visualize and communicate with our APIs directly through the browser. Moreover, please see the GroupDocs.Merger Cloud SDK for Python Examples here.

Ask a question

If you have any questions about PowerPoint merger, please feel free to ask us on the Free Support Forum.

See Also