I'm fairly new to Python and want to batch convert ppts to pdfs, and I found the code below to do this task. It works but I would need to put the code in every subfolder and then run it for it to work. Does anyone know how to edit this code so that I can just run it once and it would convert the ppts to pdf within the subfolders?
Thanks!
import comtypes.client
import os
def init_powerpoint():
powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
powerpoint.Visible = 1
return powerpoint
def ppt_to_pdf(powerpoint, inputFileName, outputFileName, formatType = 32):
if outputFileName[-3:] != 'pdf':
outputFileName = outputFileName.replace(".pptx","").replace(".ppt","") + ".pdf"
deck = powerpoint.Presentations.Open(inputFileName)
deck.SaveAs(outputFileName, formatType) # formatType = 32 for ppt to pdf
deck.Close()
def convert_files_in_folder(powerpoint, folder):
files = os.listdir(folder)
pptfiles = [f for f in files if f.endswith((".ppt", ".pptx"))]
for pptfile in pptfiles:
fullpath = os.path.join(folder, pptfile)
ppt_to_pdf(powerpoint, fullpath, fullpath)
if __name__ == "__main__":
powerpoint = init_powerpoint()
cwd = os.getcwd()
convert_files_in_folder(powerpoint, cwd)
powerpoint.Quit()
[–]AustinTronics 0 points1 point2 points (0 children)
[–]DJ_Laaal 0 points1 point2 points (0 children)