Blog
Convert powerpoint to PDF with Python and LibreOffice
Intro
In this article we are going to demonstrate how easy it is to convert a powerpoint file (ppt, pptx) to a PDF file using Python and the Api2Pdf LibreOffice API endpoint. Api2Pdf is a REST API that makes it easy to generate PDF files from HTML, URLs, and Microsoft Office files. Api2Pdf exposes an endpoint to LibreOffice, which is what is used to open the powerpoint file and convert to PDF. Rather than requiring you to install and configure LibreOffice on your own machine, or wrangle it into the cloud, this is all handled for you.
Step 1 – Installation
Install the Api2Pdf client library for python by opening opening your terminal and typing:
pip install api2pdf
You can view the full documentation for how to use the api2pdf client library viewing the README on Github here. For this article, we are just going to focus on converting a powerpoint file to PDF.
Step 2 – Choose a Powerpoint File
In order for Api2Pdf to convert a PPT to PDF, you need to provide Api2Pdf a URL to the powerpoint file. This URL should be available publicly, whether it be hosted on your own site, or a dedicated file server, like Amazon S3. Api2Pdf’s infrastructure is built on AWS Lambda in the US East (N. Virginia) region. If you want to maximum speeds, upload your PDFs to Amazon S3 in that region.
If you have no PDF readily available online and just want to try a quick test, use the following URL:
https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-power-point.pptx
Api2Pdf will consume the powerpoint file at the URL you provided and convert it to a PDF, returning to you a URL to the newly created PDF.
Step 3 – Get an API key
Create an account at https://portal.api2pdf.com to quickly grab an API key. It only takes a minute.
Step 4 – Write your code
Start with the sample code below, replacing ‘YOUR-API-KEY’ with the api key you what you retrieved from Step 3 and the URL to your own powerpoint file. In reality, you will likely be uploading many PPT files to your own file server as opposed to hard coding it in.
from api2pdf import Api2Pdf
a2p_client = Api2Pdf('YOUR-API-KEY')
url_to_ppt = 'https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-power-point.pptx'
api_response = a2p_client.LibreOffice.convert_from_url(url_to_ppt)
print(api_response['pdf'])
Run the code and visit the URL that is produced as a result!
If you have any questions, feel free to reach out to our support team.