For work, I need to convert all ppt/pptx and doc/docx files in a given folder and convert them to pdf. However, some of the files are password protected. So far I have this code:
import os
import win32com
import win32com.client
print("Please set the directory you want to scrape files from:")
FOLDER_PATH = input()
def PPTtoPDF(inputFileName, outputFileName, formatType=32):
powerpoint = win32com.client.Dispatch("Powerpoint.Application")
if inputFileName[-3:] == "ppt":
outputFileName = outputFileName[:-4] + ".pdf"
if inputFileName[-4:] == "pptx":
outputFileName = outputFileName[:-5] + ".pdf"
if (inputFileName[-3:] == "ppt") | (inputFileName[-4:] == "pptx"):
try:
deck = powerpoint.Presentations.Open(inputFileName, WithWindow=False)
deck.SaveAs(outputFileName, formatType) # formatType = 32 for ppt to pdf
deck.Close()
os.remove(inputFileName)
except:
print("PPT2PDF: There's an error" + inputFileName)
After that I have another function that grabs the filenames from the directory I've set, put it into a list, and then loops through the list to run the PPTtoPDF function.
The above function works, but I have to input the password manually every time there's a password-protected document.
I have tried the following fixes:
deck = powerpoint.Presentations.Open(inputFileName, WithWindow=False, Password="password1234")
But this turns an error:
TypeError: Open() got an unexpected keyword argument 'Password'
A similar error also often happens with the WithWindow argument, and I don't know why that is. Sometimes it works, sometimes it doesn't.
I have also tried
deck = powerpoint.Presentations.Open(inputFileName, False, True, None, "password1234")
which was suggested in How to open a password protected excel file using python? But it returns the following error:
TypeError: Open() takes from 1 to 5 positional arguments
I'm not sure what went wrong. Coding is definitely not my expertise, and the documentation for py32win seems to be scarce and very technical. Any help/suggestion is appreciated.
Thanks!