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.