pythonpowerpointwin32com

Python Win32Com: How do I open a password-protected Powerpoint?


I tried to use the below code to open a password-protected ppt, but I got error: TypeError: Open() got an unexpected keyword argument 'Password'

Is it possible to use Win32Com to open a password-protected powerrpoint? Thank you


import win32com.client as win32
xl = win32.Dispatch('PowerPoint.Application')
xl.Visible = True
xl.DisplayAlerts = True 
    
wb = xl.Presentations.Open(r'C:\Users\Downloads\PPT File.ppt', Password="123") 


Solution

  • As you can see:

    TypeError: Open() got an unexpected keyword argument 'Password'
    

    The error clearly outlines that Presentations.Open() method doesn’t have a Password parameter. Thus, passing it raises the TypeError

    To programmatically open the password-protected pptx file, you can use msoffcrypto-tool

    pip install msoffcrypto-tool
    

    It decrypts the file to a temp copy first, then opens the decrypted copy via the COM object.

    Here's an example:

    import msoffcrypto
    import tempfile
    import win32com.client as win32
    
    file_path = r"your pptx file path"
    password = "your password"
    
    # decrypt to a temp file
    with open(file_path, "rb") as f:
        office = msoffcrypto.OfficeFile(f)
        office.load_key(password=password)
        with tempfile.NamedTemporaryFile(delete=False, suffix=".pptx") as out:
            office.decrypt(out)
        decrypted_path = out.name
    
    # open the decrypted copy
    ppt = win32.Dispatch("PowerPoint.Application")
    ppt.Visible = True
    pres = ppt.Presentations.Open(decrypted_path, ReadOnly=True, WithWindow=True)
    

    output:

    reference