Python 3 Tkinter Win32com Script to Convert MS Powerpoint PPTX to PDF Documents GUI Desktop App Full Project For Beginners

Python 3 Tkinter Win32com Script to Convert MS Powerpoint PPTX to PDF Documents GUI Desktop App Full Project For Beginners

 

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

 

 

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)

 

See also  Python 3 SlimIt Library Script to Compress or Minify Javascript Source Code Full Project For Beginners

 

Now just run the python script by typing the below command as shown below

 

python app.py

 

Leave a Reply