Skip to content

Instantly share code, notes, and snippets.

@gsaltintas
Created October 18, 2021 07:02
Show Gist options
  • Save gsaltintas/70a3d1d51b84457e163b0fae811e9397 to your computer and use it in GitHub Desktop.
Save gsaltintas/70a3d1d51b84457e163b0fae811e9397 to your computer and use it in GitHub Desktop.
Convert PowerPoint to Pdf
import argparse
import os
import sys
"""
Important: Make sure to install comtypes package before running
TODO: only works in windows, find an alternative to comtypes
Adapted from the gist at https://gist.github.com/littmus/6496277
"""
try:
from comtypes import client
except:
print("Install comtypes from http://sourceforge.net/projects/comtypes/")
sys.exit(-1)
allowed_modes = ["jpg", "png", "pdf"]
if __name__ == '__main__':
parser = argparse.ArgumentParser()
# parser.add_argument('-f', '--file', required=True)
# parser.add_argument('-p', '--path', required=False)
# parser.add_argument('-m', '--mode', required=False, default="jpg")
# io_args = parser.parse_args()
# f = os.path.abspath(io_args.file)
# file_name = get_presentation_name_from_path(f)
# mode = io_args.mode.lower()
file_path = os.path.abspath(os.path.dirname(os.path.abspath(__file__)))
pres_names = [i.split('.pptx')[0] for i in os.listdir(file_path) if 'pptx' in i]
files = [os.path.join(file_path, '%s.pptx'%i) for i in pres_names if '%s.pdf' %i not in os.listdir(file_path)]
print(files)
mode = "pdf"
for f in files:
path_to_save = os.path.abspath('%s.pdf' % f.split('.pptx')[0])
if not os.path.exists(f):
print("No such file!")
sys.exit(-1)
powerpoint = client.CreateObject('Powerpoint.Application')
powerpoint.Presentations.Open(f)
powerpoint.ActivePresentation.Export(path_to_save, mode)
powerpoint.ActivePresentation.Close()
powerpoint.Quit()
print("Converting successfully finished. Resulting pngs can be found at %s" % path_to_save)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment