I. Business Context
In many situations, you do not want recipients to freely modify the main body of a Word document, but you may still want to allow form filling in certain areas, permit comments, or make the document read-only. Common examples include:
- Draft contracts sent to external parties where only designated areas should be fillable;
- Review documents where reviewers can add comments but cannot modify the original text;
- Automatically generated reports where downstream users must not accidentally change fixed template content;
- Batch-exported documents that need a consistent protection policy instead of manual setup in Word.
When there are many documents, opening each one manually to apply protection and enter a password is inefficient. With Python, you can load documents in batches, configure protection rules, and save the results to new files, making it easy to integrate protection into automated export workflows.
Note: The examples in this article use Free Spire.Doc for Python. It provides basic APIs for reading, writing, and protecting documents, but it has certain page limits. It is suitable for personal or small projects, so evaluate whether it meets your needs before use.
II. Environment Preparation
Install Dependencies
pip install spire.doc.free
Core Capabilities
Word's editing restrictions generally fall into the following categories, which are also the most common modes in everyday use:
-
Read-only protection (
AllowOnlyReading): The entire document is read-only and cannot be modified. -
Comments only (
AllowOnlyComments): Users can insert comments only; the main body cannot be changed. -
Form fields only (
AllowOnlyFormFields): Users can operate only form fields. -
Revisions only (
AllowOnlyRevisions): All modifications are retained as tracked changes. -
No protection (
NoProtection): Removes document protection.
You can also set a protection password. This password only restricts editing; it does not encrypt the file. Encrypting a document when saving is a separate feature.
III. Complete Example Code
Below are wrapper functions for several common scenarios: applying protection, removing protection, and processing documents in batches.
from spire.doc import Document, ProtectionType
def set_word_protection(
input_path: str,
output_path: str,
protection_type: ProtectionType,
password: str = ""
):
"""
Apply editing restrictions to a Word document.
:param input_path: Path to the source document
:param output_path: Output path after protection is applied
:param protection_type: Protection type
:param password: Protection password; leave empty for no password
"""
doc = Document()
doc.LoadFromFile(input_path)
# Apply document protection
doc.Protect(protection_type, password)
# Save
doc.SaveToFile(output_path)
doc.Close()
def remove_word_protection(input_path: str, output_path: str, password: str = ""):
"""Remove editing protection from a document. If a password is set, the correct password is required."""
doc = Document()
doc.LoadFromFile(input_path)
doc.Unprotect(password)
doc.SaveToFile(output_path)
doc.Close()
if __name__ == "__main__":
# Scenario 1: Set to [Comments Only] with a protection password
set_word_protection(
input_path="template.docx",
output_path="protected_comments.docx",
protection_type=ProtectionType.AllowOnlyComments,
password="Demo@2026"
)
# Scenario 2: Set to [Read Only] without password protection
# Users can still remove the protection manually.
set_word_protection(
input_path="template.docx",
output_path="protected_readonly.docx",
protection_type=ProtectionType.AllowOnlyReading
)
# Scenario 3: Remove protection
# remove_word_protection("protected_comments.docx", "unprotected.docx", password="Demo@2026")
IV. Advanced: Keep Specific Areas Editable
In many cases, you do not want to lock the entire document. Instead, you want most of it to be read-only while allowing edits to specific paragraphs or fragments. The library provides PermissionStart and PermissionEnd marker objects. If you insert a matching pair of markers into the document, the content between them is exempt from whole-document read-only protection and remains editable.
How it works: These tags mark permission ranges in a Word document. Once AllowOnlyReading protection is enabled, content between the start and end tags with the same ID automatically becomes an editable region. You can place the markers in a paragraph's child object collection to wrap either an entire paragraph or only part of one.
Example: Mark the entire second paragraph as editable while keeping the rest of the document read-only.
from spire.doc import *
from spire.doc.common import *
# Load the document
doc = Document()
doc.LoadFromFile("Sample.docx")
# Get the first section
section = doc.Sections.get_Item(0)
# Create a pair of permission markers with the same identifier, exception1
start_tag = PermissionStart(doc, "exception1")
end_tag = PermissionEnd(doc, "exception1")
# Get the target paragraph (index starts from 0; this selects the second paragraph)
target_paragraph = section.Paragraphs.get_Item(1)
# Insert the start marker at the beginning of the paragraph and the end marker at the end
target_paragraph.ChildObjects.Insert(0, start_tag)
target_paragraph.ChildObjects.Add(end_tag)
# Enable read-only protection for the whole document
doc.Protect(ProtectionType.AllowOnlyReading, "password")
# Save the result
doc.SaveToFile("output/RestrictionException.docx")
doc.Close()
V. Summary
With a few lines of Python, you can automate Word editing restrictions such as read-only, comments-only, and form-filling-only modes, replacing repetitive manual steps with a script. This approach works well for template export, batch document delivery, and automated contract pipelines.
When putting it into production, distinguish between editing protection and security. Editing protection only constrains modification behavior. If your goal is to prevent leaks or copying, you also need file encryption, watermarks, permission management, PDF conversion, and other safeguards. Do not treat document protection as your security boundary.
Top comments (0)