1. Install the PyPDF2 library
Before you start, you need to make sure that you have installed itPyPDF2
library. If you have not installed it, you can use the following command to install it through pip:
pip install PyPDF2
2. Encrypt PDF files
The process of encrypting a PDF file involves reading the original PDF file, copying its contents into a new PDF object, and applying encryption. Here is a complete sample code:
import PyPDF2 def encrypt_pdf(input_file, output_file, password): # Create PDF reader object and read input file reader = (input_file) # Create PDF write object writer = () # Add all pages from the reader object to the write object for page in : writer.add_page(page) # Open the output file and write the encrypted PDF content with open(output_file, 'wb') as f: (password) # App encryption (f) #User Exampleinput_file = '' output_file = 'encrypted_example.pdf' password = 'your_password' encrypt_pdf(input_file, output_file, password)
In the above code, we define a function called encrypt_pdf, which accepts three parameters: input file name, output file name, and password. Inside the function, we first create a PdfReader object to read the original PDF file, and then create a PdfWriter object to prepare to write to the new encrypted PDF file. By traversing the page of the PdfReader object, we add them to the PdfWriter object. Finally, the encryption is applied using the password method and the encrypted content is written to the specified output file.
3. Decrypt PDF files
The process of decrypting PDF files is similar to encryption, but there is one more step of decryption. Here is the sample code for decrypting PDF files:
import PyPDF2 def decrypt_pdf(input_pdf, output_pdf, password): # Open PDF file and create a reader object with open(input_pdf, 'rb') as file: pdf_reader = (file) # Decrypt PDF files pdf_reader.decrypt(password) # Create PDF write object pdf_writer = () # Add all pages from the reader object to the write object for page_num in range(len(pdf_reader.pages)): pdf_writer.add_page(pdf_reader.pages[page_num]) # Open the output file and write the decrypted PDF content with open(output_pdf, 'wb') as output: pdf_writer.write(output) #User Exampledecrypt_pdf('encrypted_example.pdf', 'decrypted_example.pdf', 'your_password')
In this example,decrypt_pdf
The function accepts three parameters: input the encrypted PDF file name, output the decrypted PDF file name and password. Inside the function, we first usePdfReader
The object reads the encrypted PDF file and calls itdecrypt(password)
Method to decrypt. Then, create aPdfWriter
object and add the decrypted page to the object. Finally, write the decrypted content to the specified output file.
4. Summary
passPyPDF2
library, we can easily implement the encryption and decryption of PDF files. This article describes how to use Python scripts to do these operations, including detailed steps to install libraries, encrypt PDFs, and decrypt PDFs. I hope this tutorial can help you better protect and manage the privacy and security of PDF files.
The above is the detailed content of using Python to encrypt and decrypt PDF files. For more information about Python encryption and decryption PDFs, please follow my other related articles!