Password-Protect Presentations in Python

Overview

An opening password encrypts a presentation. The correct password is required to load and view the presentation content, so this protection provides confidentiality.

An opening password is different from a write-protection password. Write protection restricts modification but does not encrypt the content or prevent the presentation from being loaded. To manage passwords for modifying presentations, see Write-Protect Presentations.

The workflows below apply to both PPT and PPTX presentations. The examples use both formats where their file-based and stream-based behavior is important.

Encrypt a Presentation with an Opening Password

Use ProtectionManager.encrypt to assign an opening password. Then use Presentation.save to persist the encrypted presentation.

The following example encrypts a PPTX presentation:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, SaveFormat

presentation = Presentation("pres.pptx")
try:
    presentation.getProtectionManager().encrypt("open_password")
    presentation.save("encrypted-pres.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

Keep Document Properties Public

By default, Aspose.Slides includes document properties in presentation encryption. The ProtectionManager.setEncryptDocumentProperties method controls this behavior independently of slide-content encryption. Pass False before calling ProtectionManager.encrypt when an indexing, classification, search, or document-management system must read metadata without the opening password.

The following example creates an encrypted PPTX presentation while leaving its built-in document properties public:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, SaveFormat

presentation = Presentation()
try:
    properties = presentation.getDocumentProperties()
    properties.setAuthor("Contoso Knowledge Management")
    properties.setTitle("Quarterly Product Roadmap")
    properties.setKeywords("roadmap, planning, internal")

    presentation.getSlides().get_Item(0).setName("Encrypted presentation content")
    presentation.getProtectionManager().setEncryptDocumentProperties(False)
    presentation.getProtectionManager().encrypt("open_password")
    presentation.save("public-properties-encrypted.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

Passing False to ProtectionManager.setEncryptDocumentProperties does not make slides, masters, layouts, shapes, media, or other presentation content public. It affects only document properties. To read those properties without loading the encrypted content, see Manage Presentation Properties.

Load an Encrypted Presentation

Set LoadOptions.setPassword to the opening password and pass the options to Presentation when loading the file. Loading fails when an opening password is required but the supplied password is missing or incorrect.

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import LoadOptions, Presentation

load_options = LoadOptions()
load_options.setPassword("open_password")

presentation = Presentation("encrypted-pres.pptx", load_options)
try:
    # Work with the decrypted presentation.
    pass
finally:
    presentation.dispose()

Remove Encryption from a Presentation

Load the presentation with its opening password, call ProtectionManager.removeEncryption, and save the result. The saved presentation can then be loaded without a password.

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import LoadOptions, Presentation, SaveFormat

load_options = LoadOptions()
load_options.setPassword("open_password")

presentation = Presentation("encrypted-pres.pptx", load_options)
try:
    presentation.getProtectionManager().removeEncryption()
    presentation.save("encryption-removed.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

Validate an Opening Password Before Loading

Use PresentationFactory.getPresentationInfo to obtain PresentationInfo without creating a complete presentation instance. Check PresentationInfo.isPasswordProtected before requesting or validating a password. When protection is present, validate the supplied value with PresentationInfo.checkPassword.

File-Path Workflow

The following example validates an opening password for a PPTX file, passes the validated value to LoadOptions.setPassword, and then loads the complete presentation:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import LoadOptions, Presentation, PresentationFactory

file_path = "protected-presentation.pptx"
password = "open_password"
presentation_info = PresentationFactory.getInstance().getPresentationInfo(file_path)

if not presentation_info.isPasswordProtected():
    print("The presentation does not have an opening password.")
elif not presentation_info.checkPassword(password):
    print("The opening password is incorrect.")
else:
    load_options = LoadOptions()
    load_options.setPassword(password)

    presentation = Presentation(file_path, load_options)
    try:
        print("The presentation was validated and loaded successfully.")
    finally:
        presentation.dispose()

Stream Workflow

The stream overload of PresentationFactory.getPresentationInfo provides the same workflow. Reset the position of a seekable stream before loading the complete presentation from that stream.

The following example uses a PPT file:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import LoadOptions, Presentation, PresentationFactory

FileInputStream = jpype.JClass("java.io.FileInputStream")

password = "open_password"

presentation_stream = FileInputStream("protected-presentation.ppt")
try:
    presentation_info = PresentationFactory.getInstance().getPresentationInfo(presentation_stream)

    if not presentation_info.isPasswordProtected():
        print("The presentation does not have an opening password.")
    elif not presentation_info.checkPassword(password):
        print("The opening password is incorrect.")
    else:
        presentation_stream.getChannel().position(0)

        load_options = LoadOptions()
        load_options.setPassword(password)

        presentation = Presentation(presentation_stream, load_options)
        try:
            print("The presentation was validated and loaded successfully.")
        finally:
            presentation.dispose()
finally:
    presentation_stream.close()

checkPassword Return Values

PresentationInfo.checkPassword returns True only when the presentation has an opening password and the supplied password is correct. It returns False in each of these cases:

  • The password is incorrect.
  • The presentation does not have an opening password.
  • The supplied password is None or empty.

The behavior is the same for PPT and PPTX presentations.

Check Whether a Loaded Presentation Is Encrypted

After loading a presentation with the correct password, inspect ProtectionManager.isEncrypted to confirm that the source presentation was encrypted. To detect opening-password protection before loading, use PresentationInfo.isPasswordProtected as shown above.

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import LoadOptions, Presentation

load_options = LoadOptions()
load_options.setPassword("open_password")

presentation = Presentation("encrypted-pres.pptx", load_options)
try:
    is_encrypted = presentation.getProtectionManager().isEncrypted()
    print(f"The presentation is encrypted: {is_encrypted}")
finally:
    presentation.dispose()

Security Recommendations

Password-Protect a Presentation Online

  1. Open the Aspose.Slides Lock application.
  2. Select or upload the presentation.
  3. Enter a password for view protection.
  4. Optionally enter a separate password for edit protection.
  5. Apply the protection and download the resulting file.

FAQ

What is the difference between an opening password and a write-protection password?

An opening password encrypts the presentation and is required to load its content. A write-protection password restricts modification without encrypting the content.

Can I validate an opening password without loading all slides?

Yes. Obtain presentation information, check whether opening-password protection is present, and validate the password before creating a complete presentation instance.

Can an application read metadata without the opening password?

Yes, but only when the presentation was encrypted with document-property encryption disabled. The application must then use the document-properties-only loading mode described in Manage Presentation Properties.

Do the password-checking workflows support both PPT and PPTX?

Yes. File-path and stream-based password detection and validation behave the same for PPT and PPTX presentations.