Welcome folks today in this blog post we will be converting ms powerpoint to pdf documents
using win32com
library in tkinter using python. All the full source code of application is shown below.
Get Started
In order to get started you need to install the following library using the pip
command as shown below
pip install win32com
After installing this library you need to make an app.py
file and copy paste the following code which is shown below
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
import sys import argparse import os import fnmatch import win32com import tkinter as Tkinter 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) |
Now just run the python script by typing the below command as shown below
python app.py