DEV Community

Cover image for How To Convert PPTX file to PDF using Python on Windows, Mac, or Linux.
EphraimX
EphraimX

Posted on

How To Convert PPTX file to PDF using Python on Windows, Mac, or Linux.

Hey there, welcome to the premier article of the troubleshooting series. In this article series, which will be released every week or every two weeks, I'll share the problems I faced while performing a task and the solutions. The articles would be very brief, as I understand that you're trying to get to the solution; hence, there is no need for many words. I'll share links and resources where necessary, so let's get started without further ado.

The Backstory

Like all other upcoming articles, there will be a backstory about what led me here. What led me here was pretty simple: I had created a PowerPoint file with the Python library PPTX and was looking for a way to convert this PPTX file to PDF so it would be easily accessible to the target audience. And so I began searching for the best method to work for my scenario.

To put a caveat out there, I'm working on a Linux machine, and the application will also be deployed on an Amazon EC2 Linux server, so I was looking for a solution that could comfortably work on a Linux machine. This may not be great news for Windows users, but it's pretty straightforward for Windows users.

PPTX to PDF on a Windows Machine

In the previous paragraph, I mentioned that converting from PPTX to PDF on a Windows machine is pretty straightforward, and the reason is pretty simple. PowerPoint as a package was initially built for Windows systems, and most packages available to facilitate this conversion use the .NET package, which is very much compatible with Windows systems.

The conversion from PPTX to PDF will require using the comptypes library. I found this solution on Stack Overflow; if you're interested in finding out more about it, here's the link.

You can install comptypes by running:

pip install comtypes
Enter fullscreen mode Exit fullscreen mode

To make the conversion, you can import the comptype library and make use of the PPTtoPDF function below:

import comtypes.client

def PPTtoPDF(inputFileName, outputFileName, formatType = 32):
    powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
    powerpoint.Visible = 1

    if outputFileName[-3:] != 'pdf':
        outputFileName = outputFileName + ".pdf"
    deck = powerpoint.Presentations.Open(inputFileName)
    deck.SaveAs(outputFileName, formatType) # formatType = 32 for ppt to pdf
    deck.Close()
    powerpoint.Quit()
Enter fullscreen mode Exit fullscreen mode

The number seen as the format type specifies the type of file to save as it is passed to the SaveAs method. If you're interested in making other conversions, you can check the complete list here.

PPTX to PDF on Mac and Linux

The conversion to PDF on Mac and Linux is also quite simple. First, you'll need to install LibreOffice. All you need to do is head over to the downloads page, select your operating system and click on download. I'm on an Ubuntu machine, so I'll choose the Linux 64-bit (Deb) option.

LibreOffice For Linux Machine

For the conversion, first, you'll need to have the subprocess library on your system; I believe this comes by default, but if it's not present on your system, you can install it using the:

pip install subprocess.run
Enter fullscreen mode Exit fullscreen mode

Next, you can run the following code, inputting your parameters where necessary.

import subprocess

def convert_pptx_to_pdf(filename):
    subprocess.run(["libreoffice", "--headless", "--convert-to", "pdf", filename])
Enter fullscreen mode Exit fullscreen mode

Here's what going on:

  • subprocess.run(...): This is a function from the subprocess module in Python that is used to spawn a new process, connect to its input/output/error pipes, and obtain its return code. It is a way to run external commands or programs from within a Python script.

  • ["libreoffice", "--headless", "--convert-to", "pdf", filename]: This is the list of arguments passed to subprocess. run, which specifies the command to execute.

    • LibreOffice: This is the command-line executable for LibreOffice, a free and open-source office suite.
    • --headless: This option runs LibreOffice without its GUI, which is necessary when running commands in a script or a server environment where a graphical user interface is not available or desirable.
    • --convert-to pdf: This option tells LibreOffice to convert the input file (specified by filename) to PDF format. The convert-to argument specifies the output format.
    • filename: This is a placeholder for the actual file name or path of the document you want to convert to PDF. This should be replaced with your code's exact file name or path.

With this, you should have the PDF version of your PPTX file in the same directory when the Python code is run or in the directory specified by you.

Conclusion

There you have it; now you can easily convert from PPTX to PDF using Python on either Windows, Mac, or Linux. These solutions are not set in stone, so if you figure out other ways to make these conversions happen, please share them in the comment section for me and others to learn from them.

Till the next troubleshooting article, stay bugging.

Top comments (2)

Collapse
 
din0i profile image
Dino

Thankyou for the solution !

Collapse
 
ephraimx profile image
EphraimX

You're welcome!