Security (2)
Protecting valuable and sensitive information from unauthorized access is a crucial task for individuals and organizations alike. When it comes to sharing and storing confidential Word documents, such as financial records, legal documents, or personal records, encrypting the documents provides extra protection for their security and confidentiality. Moreover, using Python, users can easily encrypt large numbers of Word documents. This article shows how to use Spire.Doc for Python to encrypt Word documents in Python programs.
- Encrypt a Word Document with a Password
- Change the Password of a Word Document
- Remove the Encryption from a Word Document
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: How to Install Spire.Doc for Python on Windows
Encrypt a Word Document with a Password
Using the Document.Encrypt(password: str) method provided by Spire.Doc for Python, developers can set an open password for a Word document, ensuring that only authorized people can open and view the document. The detailed steps for encrypting a Word document with a password are as follows:
- Create an instance of Document class.
- Load a Word document using Document.LoadFromFile() method.
- Encrypt the document using Document.Encrypt() method.
- Save the document using Document.SaveToFile() method.
- Python
from spire.doc import * from spire.doc.common import * # Create an instance of Document class doc = Document() # Load a Word document doc.LoadFromFile("Sample.docx") # Encrypt the document doc.Encrypt("password") # Save the document doc.SaveToFile("output/EncryptedDocument.docx") doc.Close()
Change the Encryption from a Word Document
By passing the password as the parameter, developers can load an encrypted document using Document.LoadFromFile(fileName: str, fileFormat: FileFormat, password: str) method. After loading the encrypted document, the Document.Encrypt() method can be used to set a new password. The detailed steps are as follows:
- Create an instance of Document class.
- Load an encrypted Word document using Document.LoadFromFile() method.
- Change the password of the document using Document.Encrypt() method.
- Save the document using Document.SaveToFile() method.
- Python
from spire.doc import * from spire.doc.common import * # Create an instance of Document class doc = Document() # Load an encrypted Word document doc.LoadFromFile("output/EncryptedDocument.docx", FileFormat.Docx, "password") # Change the password doc.Encrypt("password1") # Save the document doc.SaveToFile("output/ChangeDocument.docx") doc.Close()
Remove the Password from a Word Document
After loading an encrypted Word document, developers can also use Document.RemoveEncryption() method to remove the encryption from the document directly, thus making the document available to all users. The detailed steps are as follows:
- Create an instance of Document class.
- Load an encrypted Word document using Document.LoadFromFile() method.
- Remove the password using Document.RemoveEncryption() method.
- Save the document using Document.SaveToFile() method.
- Python
from spire.doc import * from spire.doc.common import * # Create an instance of Document class doc = Document() # Load an encrypted Word document doc.LoadFromFile("output/EncryptedDocument.docx", FileFormat.Auto, "password") # Remove the password doc.RemoveEncryption() # Save the document doc.SaveToFile("output/RemovePassword.docx", FileFormat.Docx) doc.Close()
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.
Python: Set or Remove Word Document Editing Restrictions
2023-09-28 01:01:07 Written by support iceblueThe 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.
- Restrict Editing of Entire Word Documents with Passwords
- Set Exceptions to Word Document Editing Restrictions
- Remove Editing Restrictions from Word Documents
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()
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()
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()
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.