OdeToCode IC Logo

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)

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

This script is using Python for Windows extensions.

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 closed.