2025, Oct 03 13:00

Open a Password-Protected PowerPoint in Python: Fix win32com TypeError and decrypt with msoffcrypto-tool

Learn why Presentations.Open lacks a Password argument in win32com and how to decrypt pptx with msoffcrypto-tool, then open it in Python PowerPoint automation.

Opening password-protected PowerPoint files through Python automation looks straightforward until you pass a parameter that the COM API doesn’t understand. If you try to feed a password directly to PowerPoint via win32com, you’ll hit a TypeError because the expected argument simply doesn’t exist. Below is a clear walkthrough of the failure mode and a practical way to open a protected deck programmatically.

Reproducing the issue

The following snippet attempts to open a protected presentation by supplying a Password keyword to Presentations.Open. It launches the PowerPoint COM application, makes it visible, and then fails at the open call.

import win32com.client as comwin
pp_app = comwin.Dispatch('PowerPoint.Application')
pp_app.Visible = True
pp_app.DisplayAlerts = True

slideset = pp_app.Presentations.Open(r'C:\Users\Downloads\PPT File.ppt', Password="123")

Python raises a TypeError like this:

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

Why it fails

The error message tells the whole story. Presentations.Open does not have a Password parameter, so passing it results in a TypeError. In other words, win32com is correctly forwarding your call to the COM method, but the method signature doesn’t support that argument. If you remove the Password argument entirely, PowerPoint will open a dialog and prompt for the password interactively, which is fine for manual use but not automation.

A working approach with msoffcrypto-tool

To open a password-protected pptx file programmatically, decrypt it first and then hand the decrypted copy to PowerPoint. The msoffcrypto-tool package handles the decryption step.

pip install msoffcrypto-tool

The flow is simple: read the encrypted file, decrypt to a temporary pptx, and then open that temporary file through the COM object.

import msoffcrypto
import tempfile
import win32com.client as comlib

src_path = r"your pptx file path"
secret_key = "your password"

# decrypt to a temp file
with open(src_path, "rb") as fh:
crypt_obj = msoffcrypto.OfficeFile(fh)
crypt_obj.load_key(password=secret_key)
with tempfile.NamedTemporaryFile(delete=False, suffix=".pptx") as tmp_handle:
crypt_obj.decrypt(tmp_handle)
plain_copy = tmp_handle.name

# open the decrypted copy
pp_ctrl = comlib.Dispatch("PowerPoint.Application")
pp_ctrl.Visible = True
deck_obj = pp_ctrl.Presentations.Open(plain_copy, ReadOnly=True, WithWindow=True)

If the supplied password is correct, the decrypted copy opens normally. If the password is wrong, the decryption step raises msoffcrypto.exceptions.InvalidKeyError with the message: The file could not be decrypted with this password.

What this means for automation

Knowing what the COM API can and cannot do saves time and avoids brittle code. There is no supported Password parameter for Presentations.Open, so relying on it will consistently fail. If you need a non-interactive workflow, decrypting before opening is the reliable route for programmatic access.

Practical guidance

Don’t pass unsupported parameters to COM methods; the API signature is authoritative. If manual entry is acceptable, omit the password and let PowerPoint prompt you. For unattended scripts and services, decrypt the protected pptx with msoffcrypto-tool and then open the resulting temporary copy via win32com. This keeps the automation predictable and avoids modal dialogs or unexpected exceptions.

The article is based on a question from StackOverflow by Peter and an answer by Ajeet Verma.