Python: Set or Remove Word Document Editing Restrictions

2023-09-28 01:01:07 Written by  support iceblue
Rate this item
(0 votes)

The editing restriction function is a feature in Word documents that allows users to control and limit the editing capabilities and editable areas. It is commonly used to protect sensitive or important document from unauthorized or substandard modifications. By applying editing restrictions, the document owner can specify what types of changes can be made and which part of the document can be edited to protect the document and facilitate collaborating, information gathering, etc. This article is going to show how to restrict Word document editing and remove document editing restrictions using Spire.Doc for Python in Python programs.

Install Spire.Doc for Python

This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.Doc

If you are unsure how to install, please refer to this tutorial: How to Install Spire.Doc for Python on Windows

Restrict Editing of Entire Word Documents with Passwords

There are four types or editing restrictions in Word documents: No changes (Read only), Tracked changes, Comments, and Filling in forms. Spire.Doc for Python provides the Document.Protect() method set editing restrictions and ProtectionType Enum to represent the restriction types.

Here is a list of the ProtectionType Enum and the corresponding editing restrictions:

Enum Editing Restriction Description
ProtectionType.AllowOnlyReading No changes (Read only) Allow reading only.
ProtectionType.AllowOnlyRevisions Tracked changes Allow tracked changes only.
ProtectionType.AllowOnlyComments Comments Allow comments only.
ProtectionType.AllowOnlyFormFields Filling in forms Allow filling out forms only.
ProtectionType.NoProtection None No restrictions.

The steps for setting editing restrictions with a password on Word documents are as follows:

  • Create an object of Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Set specified editing restrictions on the document using Document.Protect(type:ProtectionType, password:str) method.
  • Save the document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create an object of Document class
doc = Document()

# Load a Word document
doc.LoadFromFile("Sample.docx")

# Set the editing restriction type to No changes (Read only)
doc.Protect(ProtectionType.AllowOnlyReading, "password")

# Set the editing restriction type to Tracked changes
# doc.Protect(ProtectionType.AllowOnlyRevisions, "password")

# Set the editing restriction type to Comments
# doc.Protect(ProtectionType.AllowOnlyComments, "password")

# Set the editing restriction type to Filling in forms
# doc.Protect(ProtectionType.AllowOnlyFormFields, "password")

# Save the document
doc.SaveToFile("output/EditingRestrictions.docx")
doc.Close()

Python: Set or Remove Word Document Editing Restrictions

Set Exceptions to Word Document Editing Restrictions

Users can add exceptions (unrestricted areas) when setting editing restrictions on Word documents by inserting permission starting and ending tags. The details steps are as follows:

  • Create an object of Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Get the first section using Document.Sections.get_Item() method.
  • Create an object of PermissionStart class and an object of PermissionEnd class.
  • Insert the permission start tag and the end tag to the document using Paragraph.ChildObjects.Insert() method and Paragraph.ChildObjects.Add() method.
  • Set the editing restriction using Document.Protect() method.
  • Save the document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create an object of Document class
doc = Document()

# Load a Word document
doc.LoadFromFile("Sample.docx")

# Get the first section
section = doc.Sections.get_Item(0)

# Create a permission start tag and an end tag
start = PermissionStart(doc, "exception1")
end = PermissionEnd(doc, "exception1")

# Insert the permission start tag and the end tag to the first section
paragraph = section.Paragraphs.get_Item(1)
paragraph.ChildObjects.Insert(0,start)
paragraph.ChildObjects.Add(end)

# Set the editing restriction
doc.Protect(ProtectionType.AllowOnlyReading, "password")

# Save the document
doc.SaveToFile("output/RestrictionException.docx")
doc.Close()

Python: Set or Remove Word Document Editing Restrictions

Remove Editing Restrictions from Word Documents

To remove the editing restrictions of a document, simply set the editing restriction type to no restriction using the Document.Protect() method. The detailed steps are as follows:

  • Create an object of Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Remove the restriction by setting the restriction type to None using Document.Protect() method.
  • Save the document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create an object of Document class
doc = Document()

# Load a Word document
doc.LoadFromFile("output/EditingRestrictions.docx")

# Remove the editing restriction by set the restriction type to None
doc.Protect(ProtectionType.NoProtection)

# Save the document
doc.SaveToFile("output/RemoveEditingRestriction.docx")
doc.Close()

Python: Set or Remove Word Document Editing Restrictions

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Thursday, 25 April 2024 02:09