Convert A Directory Of PowerPoint Slides To PDF With Python

Wednesday, June 26, 2013

There are many ways to automate Microsoft Office programs, but Python might be the prettiest:

import sys
import os
import glob
import win32com.client

def convert(files, formatType = 32):
    powerpoint = win32com.client.Dispatch("Powerpoint.Application")
    powerpoint.Visible = 1
    for filename in files:
        newname = os.path.splitext(filename)[0] + ".pdf"
        deck = powerpoint.Presentations.Open(filename)        
        deck.SaveAs(newname, formatType)
        deck.Close()
    powerpoint.Quit()

files = glob.glob(os.path.join(sys.argv[1],"*.ppt?"))
convert(files)

This script is using Python for Windows extensions.


Comments
gravatar Ankur Wednesday, June 26, 2013
Is it possible to convert say doc to pdf using windows extensions ?
gravatar Scott Wednesday, June 26, 2013
Yes, just need to use the Word object model API instead of Powerpoint. http://msdn.microsoft.com/en-us/library/kw65a0we(v=vs.80).aspx
gravatar bogd Friday, July 26, 2013
A big thank you for this! I found your post only after I spent an entire afternoon trying to get my head around the Powerpoint API (and trying to figure out what the correct number is for the PDF format :) ). I wish I'd found it sooner... :)
Comments are now closed.
by K. Scott Allen K.Scott Allen
My Pluralsight Courses
The Podcast!