Unprotect Word Documents using Python

In various cases, the Word documents are protected with a password before they are shared. On receiving the protected documents, you need to unlock them to access their content. To perform the unlocking programmatically, this article covers how to unprotect a password-protected Word document using Python. Moreover, we will cover how to remove restrictions applied to a Word document dynamically.

Python Library to Unprotect Word Documents

To unlock the Word documents programmatically, we will use Aspose.Words for Python. It is a feature-rich library that facilitates you in creating new Word documents from scratch and manipulating the existing ones. You can install the library in your Python applications from PyPI using the following pip command.

pip install aspose-words 

Unprotect a Word Document in Python

To unprotect a Word document, you would need to provide its password. The following are the steps to unprotect a Word document in Python.

  • Load the password-protected Word document by specifying its path and password in Document constructor.
  • Save the document using Document.save(string) method.

The following code sample shows how to load a password-protected Word document and save it as an unprotected document.

Remove Restrictions in a Word Document using Python

MS Word also allows you to protect the documents by applying different restrictions. For example, you can make the document read-only, allow comments or revisions only, and so on. The following steps show how to remove restrictions in a Word document using Python.

  • Load the password-protected Word document by specifying its path and password in Document constructor.
  • Call Document.unprotect() method to remove restrictions.
  • Save the updated document using Document.save(string) method.

The following code sample shows how to unprotect a Word document by removing the restrictions.

Get a Free API License

You can get a temporary license to use Aspose.Words for Python without evaluation limitations.

Conclusion

In this article, you have learned how to unprotect Word documents in Python. Moreover, you have seen how to remove restrictions applied to a Word document. You can explore more about Aspose.Words for Python using the documentation. In case you would have any queries, feel free to ask us on our forum.

See Also