Skip to content

Instantly share code, notes, and snippets.

@billyeh
Last active August 29, 2015 14:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billyeh/3e1d9b495a605b95e533 to your computer and use it in GitHub Desktop.
Save billyeh/3e1d9b495a605b95e533 to your computer and use it in GitHub Desktop.
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))
excel_dispatch.Quit()
for f in files:
for entry in f:
print_file(entry, ppt_dispatch)
ppt_dispatch.Quit()
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:
ppt.Slides(slide).Delete()
if not os.path.exists(outputDirectory):
os.makedirs(outputDirectory)
print('Printing slides {} from {} to {}'.format(slideRange, pptFileName, outputFileName))
# PpFixedFormatType for PDFs is 32
ppt.SaveAs(pdf_name, 32)
ppt.Close()
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
wb.Close()
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
ret.append(int(elem[0]))
elif len(elem) == 2: # a range inclusive
start, end = map(int, elem)
for i in xrange(start, end+1):
ret.append(i)
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')
b.pack()
fileName = askdirectory()
if not fileName:
exit()
fileName = os.path.normpath(fileName)
main(fileName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment