This simple article is designed to guide you on how to decrypt PDF file using Python. It encompasses crisp information to decrypt PDF using Python in the form of a step-wise process and a runnable sample code. The resultant unencrypted PDF file can be either saved on the disk or to a stream in any desired format like PDF, HTML, DOCX, etc.
Steps to Decrypt Password Protected PDF using Python
- Set the IDE to use Aspose.PDF for Python via .NET in order to decrypt a PDF file
- Access the encrypted PDF file into the Document class object using the file name and password
- Use the Document.decrypt method to unencrypt the PDF file
- Save the unencrypted PDF file on the disk with a new name
These steps cover a simple process to unencrypt PDF using Python such that first of all the environment configuration is done. In the next step, a password protected source PDF file is loaded from the disk using an instance of the Document class by providing a valid password. Then the file is decrypted by calling the decrypt() method in the Document class. You may use either the owner password or the user password based on the application requirement as with the owner password you have complete control over the decrypted file whereas, with the user password, it can be granted a read-only access.
Code to Decrypt PDF File using Python
This example code exhibits how to unencrypt PDF using Python using a very simple API interface. You have a number of choices to save the output PDF file to different formats provided by the save_format enumerator along with the provision to set a warning handler during the process of saving the file and setting the flag to close the response object once the PDF document is saved to response object inside a web-based application.
This topic has taught us to save an encrypted PDF as unencrypted using Python. If you want to learn to compress a PDF file, refer to the article on how to compress PDF in Python.