Skip to content

Instantly share code, notes, and snippets.

@groupdocs-cloud-gists
Last active July 21, 2022 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groupdocs-cloud-gists/4208d6166e8e2781dc936fc33909265d to your computer and use it in GitHub Desktop.
Save groupdocs-cloud-gists/4208d6166e8e2781dc936fc33909265d to your computer and use it in GitHub Desktop.
How to Convert PowerPoint to PDF using REST API in Python

Learn how to convert powerpoint file to PDF document in Python using REST API:

The following topics shall be covered in this article:

  1. PowerPoint to PDF Conversion REST API - Installation
  2. Convert PowerPoint PPTX to PDF using REST API in Python
  3. Convert Range of Pages from PPTX to PDF in Python
  4. Convert Specific Pages of PPTX to PDF in Python
  5. Convert PPTX to PDF - Online and Free
# Load Python SDK http://api.groupdocs.cloud in your python application
import groupdocs_conversion_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 different configurations
configuration = groupdocs_conversion_cloud.Configuration(client_id, client_secret)
configuration.api_base_url = "https://api.groupdocs.cloud"
my_storage = "LocalStorage"
# API initialization to download converted file
import shutil
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# Create download file request
request = groupdocs_conversion_cloud.DownloadFileRequest("python-testing\\sample-powerpoint.pdf", my_storage)
# Download converted file
response = file_api.download_file(request)
# Move the downloaded file to your local system directory
shutil.move(response, "H:\\groupdocs-cloud-data\\")
# Upload powerpoint .pptx file to your cloud storage
# Create an instance of the File API
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# Call upload file request
request = groupdocs_conversion_cloud.UploadFileRequest("python-testing\sample-powerpoint.pptx", "H:\\groupdocs-cloud-data\\powerpoint-slides.pptx", my_storage)
# Upload powerpoint slides to the cloud
response = file_api.upload_file(request)
print(response.uploaded)
# How to Convert PPTX to PDF using REST API in Python
try:
# Create an instance of the API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "python-testing/sample-powerpoint.pptx"
settings.format = "pdf"
settings.output_path = "python-testing"
# Create convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert .pptx file to PDF document
result = convert_api.convert_document(request)
print("PPTX File converted to PDF successfully: " + result[0].path)
except groupdocs_conversion_cloud.ApiException as e:
print("Exception when calling convert_document: {0}".format(e.message))
# How to Convert Range of Pages from PPTX to PDF in Python
# Create an instance of API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "python-testing/sample-powerpoint.pptx"
settings.format = "pdf"
settings.output_path = "python-testing"
# PDF convert options: start page number and total pages to convert
convertOptions = groupdocs_conversion_cloud.PdfConvertOptions()
convertOptions.from_page = 2
convertOptions.pages_count = 4
settings.convert_options = convertOptions
# Create convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert pages of pptx file to PDF file
result = convert_api.convert_document(request)
print("Converted range of pages from PPTX to PDF: " + result[0].path)
# How to Convert Specific Pages of PPTX to PDF in Python
# Create an instance of API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "python-testing/sample-powerpoint.pptx"
settings.format = "pdf"
settings.output_path = "python-testing"
# PDF convert options: page numbers to convert
convertOptions = groupdocs_conversion_cloud.PdfConvertOptions()
convertOptions.pages = [1, 3, 5]
settings.convert_options = convertOptions
# Create convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert powerpoint file to PDF in python
result = convert_api.convert_document(request)
print("Successfully converted PPTX file pages to PDF: " + result[0].path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment