Create a gist now

Instantly share code, notes, and snippets.

@billyeh /pptToPdf
Last active Aug 29, 2015

What would you like to do?
Powerpoint to PDF Converter
import sys
import argparse
import os
import fnmatch
import win32com
from Tkinter import *
from tkFileDialog import *
from win32com.client import Dispatch
def main(directory):
"""Publish Powerpoints to PDFs
This program looks for any Excel files in its subdirectories. It reads them
expecting each row to have a format of (PptFile, PageRange, PDFOutput,
OutputDirectory). Then assuming that each Powerpoint file is in the same
folder as the Excel file, it will print a PDF of that range of slides from
the Powerpoint to the output folder with the PDFOutput name specified.
files = []
excel_dispatch = Dispatch('Excel.Application')
ppt_dispatch = Dispatch('Powerpoint.Application')
for root, dirnames, filenames in os.walk(directory):
# [!~] in filter is for Windows temporary files
for filename in fnmatch.filter(filenames, '[!~]*.xls*'):
print('Reading data from {}'.format(filename))
files.append(read_excel_file(os.path.join(root, filename),
excel_dispatch, root))
for f in files:
for entry in f:
print_file(entry, ppt_dispatch)
def print_file(entry, dispatch):
pptFileName, slideRange, outputFileName, rootDirectory, outputDirectory = entry
ppt = dispatch.Presentations.Open(os.path.join(rootDirectory, pptFileName),
True, True, False)
pdf_name = os.path.join(outputDirectory, outputFileName) + '.pdf'
if slideRange:
slides_to_keep = ranges_to_list(slideRange)
for slide in list(range(1, ppt.Slides.Count + 1))[::-1]:
if slide not in slides_to_keep:
if not os.path.exists(outputDirectory):
print('Printing slides {} from {} to {}'.format(slideRange, pptFileName, outputFileName))
# PpFixedFormatType for PDFs is 32
ppt.SaveAs(pdf_name, 32)
def read_excel_file(filename, dispatch, root):
wb = dispatch.Workbooks.Open(filename)
sheet = wb.Sheets(1)
row = 1
data = []
while sheet.Cells(row, 1).Value:
data.append((sheet.Cells(row, 1).Value, sheet.Cells(row, 2).Value,
sheet.Cells(row, 3).Value, root, sheet.Cells(row, 4).Value))
row += 1
return data
def ranges_to_list(s):
"""Return each integer from a complex range string like "1-9,12, 15-20,23"
>>> list(hyphen_range('1-9,12, 15-20,23'))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 15, 16, 17, 18, 19, 20, 23]
>>> list(hyphen_range('1-9,12, 15-20,2-3-4'))
Traceback (most recent call last):
ValueError: format error in 2-3-4
ret = []
for x in s.split(','):
elem = x.split('-')
if len(elem) == 1: # a number
elif len(elem) == 2: # a range inclusive
start, end = map(int, elem)
for i in xrange(start, end+1):
else: # more than one hyphen
raise ValueError('format error in {}'.format(x))
return ret
if __name__ == '__main__':
master = Tk()
b = Label(master, text='Close the command prompt to stop the program')
fileName = askdirectory()
if not fileName:
fileName = os.path.normpath(fileName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment