How to Convert Microsoft PowerPoint to PDF with Python

Learn how to convert PowerPoint files to PDFs using pdfRest Convert to PDF API Tool with Python
Share this page

Why Convert PowerPoint to PDF with Python?

The pdfRest Convert to PDF API Tool is a powerful resource for developers who need to automate the conversion of various file types to PDF. By leveraging this tool through Python, one can seamlessly integrate PDF conversion capabilities into their applications or workflows. This tutorial will guide you through the process of sending an API call to Convert to PDF with Python, showcasing how to transform files such as images, documents, and more into PDF format programmatically.

Imagine you are working on an application that handles document archival. Users upload documents in various formats, but for consistency and ease of access, you want to store all documents as PDFs. Using the Convert to PDF API, you can automatically convert incoming files, including PowerPoint presentations, to PDF, ensuring that all documents in your archive are in a uniform, widely accepted, and secure format. This not only improves the user experience but also streamlines the document management process.

Convert PowerPoint to PDF with Python Code Example

from requests_toolbelt import MultipartEncoder
import requests
import json

pdf_endpoint_url = 'https://api.pdfrest.com/pdf'

mp_encoder_pdf = MultipartEncoder(
    fields={
        'file': ('file_name.tif', open('/path/to/file.ppt', 'rb'), 'application/vnd.ms-powerpoint'),
        'output' : 'example_pdf_out',
    }
)

headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_pdf.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
}

print("Sending POST request to pdf endpoint...")
response = requests.post(pdf_endpoint_url, data=mp_encoder_pdf, headers=headers)

print("Response status code: " + str(response.status_code))

if response.ok:
    response_json = response.json()
    print(json.dumps(response_json, indent = 2))
else:
    print(response.text)

Reference to the code source: pdfRest API Samples on GitHub.

Breaking Down the Code

The code provided is a Python script that uses the requests library to make an API call to the pdfRest Convert to PDF endpoint. Let's break down the key parts of the code:

from requests_toolbelt import MultipartEncoder
import requests
import json

This imports the necessary libraries: MultipartEncoder for creating multipart/form-data payloads, requests for making HTTP requests, and json for handling JSON data.

mp_encoder_pdf = MultipartEncoder(
    fields={
        'file': ('file_name.tif', open('/path/to/file.ppt', 'rb'), 'application/vnd.ms-powerpoint'),
        'output' : 'example_pdf_out',
    }
)

This creates a MultipartEncoder object with the fields for the API request. The 'file' field contains a tuple with the name of the file, the file object itself, and its MIME type. The 'output' field specifies the desired name for the output PDF file.

headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_pdf.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
}

The headers include the Accept header, specifying that the response should be JSON, the Content-Type header, which is set automatically by the MultipartEncoder, and the Api-Key header, where you should place your own API key.

response = requests.post(pdf_endpoint_url, data=mp_encoder_pdf, headers=headers)

This sends a POST request to the pdfRest API endpoint with the encoded data and headers.

if response.ok:
    response_json = response.json()
    print(json.dumps(response_json, indent = 2))
else:
    print(response.text)

If the request is successful, the script prints the JSON response, nicely formatted. Otherwise, it prints the error text.

Beyond the Tutorial

In this tutorial, we've accomplished sending a multipart/form-data POST request to the pdfRest Convert to PDF endpoint using Python. This allows you to convert files to PDF format programmatically. To further explore and demo all of the pdfRest API Tools, visit the API Lab. For more detailed information on the API, refer to the API Reference Guide.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at pdfRest API Samples on GitHub.

Generate a self-service API Key now!

Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.

Compare Plans
Contact Us