DEV Community

Allen Yang
Allen Yang

Posted on

How to Encrypt and Protect Word Documents with Python

How to Encrypt and Protect Word Documents with Python

Word documents often contain sensitive content such as contracts, financial statements, or internal policies. When sharing these files, you frequently need to restrict access — either requiring a password to open the document, or allowing reading only while blocking edits. Setting these options manually in Word is tedious, and it becomes impractical when distributing many files at once. Python can handle this in a unified way: a single script can add passwords, apply editing restrictions, and even leave specific paragraphs editable across a whole batch of documents.

This article demonstrates how to encrypt Word documents, apply editing protection, define editable ranges, and remove protection using Spire.Doc for Python.

Setting Up the Environment

Install the Spire.Doc library:

pip install Spire.Doc
Enter fullscreen mode Exit fullscreen mode

Then import the required modules in your script:

from spire.doc import *
from spire.doc.common import *
Enter fullscreen mode Exit fullscreen mode

Encrypting a Document with a Password

The most direct form of protection is setting an open password. Only users who enter the correct password can view the content. Encryption is done with the Encrypt() method:

# Create a Document object and load a file
document = Document()
document.LoadFromFile("contract_template.docx")

# Set the open password
document.Encrypt("E-iceblue")

# Save the encrypted document
document.SaveToFile("contract_encrypted.docx", FileFormat.Docx)
document.Close()
Enter fullscreen mode Exit fullscreen mode

After encryption, opening the file prompts for a password. The string passed to Encrypt() is the open password, and the document is written to disk in encrypted form when saved.

Restricting Editing with Protection Types

If the document should be viewable but not modifiable, use the Protect() method. It takes two arguments: the protection type and a password. The password is required to remove protection, and the protection type is specified by the ProtectionType enum.

document = Document()
document.LoadFromFile("meeting_minutes.docx")

# Allow reading only; modifications require the password
document.Protect(ProtectionType.AllowOnlyReading, "123456")

document.SaveToFile("minutes_readonly.docx", FileFormat.Docx2013)
document.Close()
Enter fullscreen mode Exit fullscreen mode

ProtectionType offers several levels of granularity:

  • AllowOnlyReading: reading only; all editing operations are blocked;
  • AllowOnlyFormFields: only form fields can be filled, suitable for documents that need input;
  • NoProtection: removes all protection.

The choice of protection type depends on how the document will be used. For example, a notice that needs to collect information can work well with AllowOnlyFormFields.

Leaving Editable Ranges in a Protected Document

In some scenarios, the whole document should be read-only, but specific paragraphs must remain editable — for instance, a signature line in a contract or answer areas in a survey. The approach is to protect the document first, then wrap the editable paragraphs with PermissionStart and PermissionEnd tags:

document = Document()
document.LoadFromFile("survey.docx")

# Protect the whole document first
document.Protect(ProtectionType.AllowOnlyReading, "password")

# Create a pair of start and end tags for the editable range
start = PermissionStart(document, "editID")
end = PermissionEnd(document, "editID")

# Insert the tags at the beginning and end of the first paragraph
first_paragraph = document.Sections[0].Paragraphs[0]
first_paragraph.ChildObjects.Insert(0, start)
first_paragraph.ChildObjects.Add(end)

document.SaveToFile("survey_editable.docx", FileFormat.Docx)
document.Close()
Enter fullscreen mode Exit fullscreen mode

PermissionStart and PermissionEnd share the same ID to identify one range; the content between them is the editable area. Readers can then modify the marked paragraphs while the rest of the document stays protected.

Locking Specific Sections

For documents made up of multiple sections, you can protect only part of them. The idea is to enable protection on the whole document first, then set ProtectForm = False on the sections that should remain editable:

document = Document()

# Add two sections with text
s1 = document.AddSection()
s2 = document.AddSection()
s1.AddParagraph().AppendText("Section 1: protected content")
s2.AddParagraph().AppendText("Section 2: editable content")

# Protect the whole document with the form fields protection type
document.Protect(ProtectionType.AllowOnlyFormFields, "123")

# Unprotect section 2
s2.ProtectForm = False

document.SaveToFile("section_protection.docx", FileFormat.Docx2013)
document.Close()
Enter fullscreen mode Exit fullscreen mode

A typical use case is a document whose first part contains fixed instructions, while the second part holds forms or content meant for the reader to fill in.

Opening an Encrypted Document and Removing Protection

To modify a protected document later, load it with the password and then remove the protection:

document = Document()

# Load the encrypted document with its password
document.LoadFromFile("contract_encrypted.docx", FileFormat.Docx, "E-iceblue")

# Remove all protection
document.Protect(ProtectionType.NoProtection)

document.SaveToFile("contract_decrypted.docx", FileFormat.Docx)
document.Close()
Enter fullscreen mode Exit fullscreen mode

The third argument of LoadFromFile() is the document's open password. Calling Protect(ProtectionType.NoProtection) afterwards removes the editing restrictions.

Practical Tips

  • Passwords are stored only in the generated file; the script itself does not record them. If a password is lost, the encrypted document cannot be opened normally, so manage passwords outside the script.
  • Protect() and Encrypt() can be combined: set an open password first, then apply editing protection, forming a double layer of defense.
  • For batch processing, put passwords and file paths in a list or loop to encrypt an entire batch of documents at once.

Conclusion

This article walked through the complete workflow of encrypting Word documents, applying editing protection, defining editable ranges, protecting individual sections, and removing protection with Python. The core APIs are Encrypt(), Protect(), PermissionStart / PermissionEnd, and the ProtectForm property. In real projects, these operations can be wrapped into functions and applied to batches of documents in a loop, replacing a large amount of repetitive manual work with a consistent security policy.

Top comments (0)