How to convert a .pptx to .pdf using Python How to convert a .pptx to .pdf using Python windows windows

How to convert a .pptx to .pdf using Python


I found the answer with the help of this post and the answer from this question.

Note that comtypes is only available for Windows. Other platforms will not support this.

import comtypes.clientdef 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()


I was working with this solution but I needed to search all .pptx, .ppt, and then turn them all to .pdf (python 3.7.5). Hope it works...

import osimport win32com.clientppttoPDF = 32for root, dirs, files in os.walk(r'your directory here'):    for f in files:        if f.endswith(".pptx"):            try:                print(f)                in_file=os.path.join(root,f)                powerpoint = win32com.client.Dispatch("Powerpoint.Application")                deck = powerpoint.Presentations.Open(in_file)                deck.SaveAs(os.path.join(root,f[:-5]), ppttoPDF) # formatType = 32 for ppt to pdf                deck.Close()                powerpoint.Quit()                print('done')                os.remove(os.path.join(root,f))                pass            except:                print('could not open')                # os.remove(os.path.join(root,f))        elif f.endswith(".ppt"):            try:                print(f)                in_file=os.path.join(root,f)                powerpoint = win32com.client.Dispatch("Powerpoint.Application")                deck = powerpoint.Presentations.Open(in_file)                deck.SaveAs(os.path.join(root,f[:-4]), ppttoPDF) # formatType = 32 for ppt to pdf                deck.Close()                powerpoint.Quit()                print('done')                os.remove(os.path.join(root,f))                pass            except:                print('could not open')                # os.remove(os.path.join(root,f))        else:            pass

The try and except was for those documents I couldn't read and won't exit the code until the last document. And I would recommend doing each type of format aside: first .pptx and then .ppt (or viceversa).


I need a way to save PPTX file to PDF and PDF with notes. Here is my solution

from comtypes.client import CreateObject, Constantsdef PPTtoPDF(inputFileName, outputFileName, formatType = 32):    powerpoint = CreateObject('Powerpoint.Application')    constants = Constants(powerpoint)    powerpoint.Visible = 1    if outputFileName[-3:] != 'pdf':        outputFileName = outputFileName + ".pdf"    deck = powerpoint.Presentations.Open(inputFileName)    deck.SaveAs(outputFileName, constants.PpSaveAsPDF)    deck.Close()    powerpoint.Quit()def PPTtoPDFNote(inputFileName, outputFileName, formatType = 32):    powerpoint = CreateObject('Powerpoint.Application')    constants = Constants(powerpoint)    powerpoint.Visible = 1    if outputFileName[-3:] != 'pdf':        outputFileName = outputFileName + ".pdf"    deck = powerpoint.Presentations.Open(inputFileName)    deck.ExportAsFixedFormat(        outputFileName,        constants.ppFixedFormatTypePDF,        constants.ppFixedFormatIntentPrint,        False, # No frame        constants.ppPrintHandoutHorizontalFirst,        constants.ppPrintOutputNotesPages,        constants.ppPrintAll    )    deck.Close()    powerpoint.Quit()

To use it,

PPTtoPDF    ('.\\Test.pptx', '.\Test.pdf'          )PPTtoPDFNote('.\\Test.pptx', '.\Test_with_Note.pdf')

Note: It is always the best to do it using Windows platform, i.e., using comtypes so that it could always support new format and features in Microsoft Powerpoint.