Protect Word Documents using Python

Protect Word Documents in Python

The information stored in Word documents is always prone to be accessed illegally. To avoid unauthorized access and forging while transmitting the documents, a protection mechanism must be applied. In this article, you will learn how to protect Word documents with a password in Python. Moreover, the article will cover how to apply different protection types to a Word document dynamically.

Python Library to Protect Word Documents

In order to password protect DOCX, DOC, and other Word formats, we’ll use Aspose.Words for Python. It is a Python library to create and manipulate Word documents dynamically. The said library is hosted on PyPI and can be installed using the following pip command.

pip install aspose-words

Protect a Word Document with Password in Python

Aspose.Words makes it quite easier for you to protect a Word document. Simply load the document, set a password, and save the protected document. The following are the steps to password protect a Word DOCX file in Python.

  • First, load the Word document using Document class.
  • Create an object of OoxmlSaveOptions class (to save in DOC format, use DocSaveOptions class instead).
  • Set password using OoxmlSaveOptions.password property.
  • Finally, save the protected Word document using Document.save(string, OoxmlSaveOptions) method.

The following code sample shows how to password protect a Word document.

The following dialogue appears when you open the password-protected Word document.

password-protected Word document in Python

Make a Word Document Read Only in Python

In certain cases, you may only need to protect the modification of the content in a Word document. In such cases, you can make the document read-only. The following steps show how to make a Word document read-only in Python.

  • First, load the Word document using Document class.
  • Use Document.protect(ProtectionType.READ_ONLY, string) method to make the Word document read-only.
  • Finally, save the protected Word document using Document.save(string) method.

The following code sample shows how to protect a Word document and make it read-only.

Other Protection Types for Word Documents

Aspose.Words for Python also allows you to use other protection types to protect a Word document. For example, you can allow comments or revisions only. The following is the list of protection types you can apply to a Word document using Aspose.Words for Python.

  • ALLOW_ONLY_COMMENTS – To allow adding comments only.
  • ALLOW_ONLY_FORM_FIELDS – To allow accessing form fields only.
  • ALLOW_ONLY_REVISIONS – To allow revisions only.
  • READ_ONLY – To make the document read only.
  • NO_PROTECTION – No protection.

To learn more about document protection using Aspose.Words for Python, visit documentation.

Get a Free API License

You can use a free temporary license to avoid evaluation limitations of Aspose.Words for Python.

Conclusion

In this article, you have learned how to protect Word documents in Python. Moreover, you have seen how to restrict the editing of Word documents using different protection types. Besides, to explore more about Aspose.Words for Python, visit the documentation. In case you would have any queries, feel free to ask us on our forum.

See Also