How to Convert PDF to PowerPoint with Python

Learn how to use Python to call the PDF to PowerPoint API Tool from pdfRest and quickly turn your PDF formatted documents into PowerPoint files.
Share this page

Why Convert PDF to PowerPoint with Python?

The pdfRest PDF to PowerPoint API Tool allows users to convert PDF documents into PowerPoint presentations programmatically. This is particularly useful for professionals who need to quickly turn reports, charts, or any other PDF content into slides for presentations. For instance, a financial analyst might convert a PDF report into a PowerPoint to present findings to stakeholders.

This tutorial will guide you through the process of sending an API call to the PDF to PowerPoint API tool using Python, enabling you to automate document conversions within your applications or workflows.

PDF to PowerPoint with Python Code Example

from requests_toolbelt import MultipartEncoder
import requests
import json

powerpoint_endpoint_url = 'https://api.pdfrest.com/powerpoint'

# The /powerpoint endpoint can take a single PDF file or id as input.
# This sample demonstrates converting a PDF to a PowerPoint document.
mp_encoder_powerpoint = MultipartEncoder(
    fields={
        'file': ('file_name', open('/path/to/file', 'rb'), 'application/pdf'),
        'output' : 'example_powerpoint_out',
    }
)

# Let's set the headers that the PowerPoint endpoint expects.
# Since MultipartEncoder is used, the 'Content-Type' header gets set to 'multipart/form-data' via the content_type attribute below.
headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_powerpoint.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

print("Sending POST request to powerpoint endpoint...")
response = requests.post(powerpoint_endpoint_url, data=mp_encoder_powerpoint, 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)

# If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.py' sample.

Source: pdf-rest-api-samples on GitHub

Breaking Down the Python Code

The provided code snippet demonstrates how to use the PDF to PowerPoint API tool with Python:

from requests_toolbelt import MultipartEncoder
import requests
import json

These lines import the necessary Python libraries. MultipartEncoder is used for creating multipart/form-data payloads, requests for making HTTP requests, and json for handling JSON data.

mp_encoder_powerpoint = MultipartEncoder(
    fields={
        'file': ('file_name', open('/path/to/file', 'rb'), 'application/pdf'),
        'output' : 'example_powerpoint_out',
    }
)

This code initializes a MultipartEncoder with the PDF file to be converted and the desired output file name.

headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_powerpoint.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

Headers are set for the HTTP request, including the API key for authentication (replace 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' with your actual API key).

response = requests.post(powerpoint_endpoint_url, data=mp_encoder_powerpoint, headers=headers)

The requests.post function sends the POST request to the API endpoint with the prepared data and headers.

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

The response is checked for success. If successful, the JSON response is printed; otherwise, the error text is displayed.

Beyond the Tutorial

In this tutorial, you've learned how to convert a PDF to a PowerPoint presentation using the pdfRest API with Python. By sending a multipart/form-data request, the API processes the PDF file and returns a JSON response with information about the converted PowerPoint file.

To explore all of the pdfRest API Tools, you can demo them in the API Lab. For more detailed information and additional endpoints, refer to the API Reference documentation.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at pdf-rest-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.