# How to Password-Protect a PDF Document Using Python

Password-protecting PDF [documents](https://bytescrum.com/) is an essential practice for safeguarding sensitive information. By [encrypting](https://blog.bytescrum.com/encrypting-and-decrypting-data-with-fernet-in-python) a PDF with a password, you can control who can open, view, and modify the document. [Python](https://blog.bytescrum.com/series/python-series) offers several libraries that make it easy to add password protection to PDF files. In this guide, we’ll show you how to use `PyPDF2` to encrypt a PDF with a password.

### **Why Password-Protect PDF Documents?**

* **Confidentiality**: Ensure only authorized users can access sensitive information.
    
* **Integrity**: Prevent unauthorized modifications to the document.
    
* **Compliance**: Meet regulatory requirements for data protection.
    

### **Getting Started**

To password-protect a PDF, you’ll need to install the `PyPDF2` library, which provides tools for reading, merging, and encrypting PDF files.

#### **Step 1: Install PyPDF2**

Install the `PyPDF2` library using pip:

```bash
pip install PyPDF2
```

#### **Step 2: Encrypt a PDF with a Password**

Here’s a simple script to add password protection to a PDF file:

```python
from PyPDF2 import PdfReader, PdfWriter

def password_protect_pdf(input_pdf, output_pdf, password):
    # Create a PdfReader object to read the input PDF
    reader = PdfReader(input_pdf)
    # Create a PdfWriter object to write the output PDF
    writer = PdfWriter()

    # Add all pages from the input PDF to the writer
    for page_num in range(len(reader.pages)):
        writer.add_page(reader.pages[page_num])

    # Encrypt the PDF with the given password
    writer.encrypt(password)

    # Write the encrypted PDF to a new file
    with open(output_pdf, "wb") as output_file:
        writer.write(output_file)

    print(f"Password-protected PDF saved as: {output_pdf}")

if __name__ == "__main__":
    input_pdf = "example.pdf"  # Path to the input PDF file
    output_pdf = "protected_example.pdf"  # Path to the output PDF file
    password = "mypassword"  # Password to protect the PDF

    password_protect_pdf(input_pdf, output_pdf, password)
```

### **How This Script Works**

1. **Importing Libraries**: The script uses `PdfReader` to read the input PDF and `PdfWriter` to create a new PDF with encryption.
    
2. **Reading the Input PDF**: The `PdfReader` object loads the input PDF file.
    
3. **Creating an Encrypted PDF**:
    
    * The script iterates through all the pages of the input PDF using `reader.pages`.
        
    * It adds each page to the `writer` object.
        
    * `writer.encrypt(password)` applies the password protection to the PDF.
        
4. **Saving the Encrypted PDF**: The encrypted PDF is saved to a new file with `writer.write(output_file)`.
    

#### **Step 3: Run the Script**

Replace the `input_pdf`, `output_pdf`, and `password` variables with your desired values and run the script. The script will generate a password-protected PDF file with the specified password.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724757963595/cf6b59c7-8413-4bc1-be3a-c40a9f7e4815.png align="center")

### **Additional Tips**

* **Choosing a Strong Password**: Use a combination of letters, numbers, and special characters to create a strong password.
    
* **Permissions**: You can customize the permissions for the encrypted PDF, such as allowing printing or copying, by modifying the `encrypt()` method's arguments. For example, `writer.encrypt(password, user_pwd=None, owner_pwd=None, use_128bit=True, permissions=None)` allows you to specify different user and owner passwords and set permissions.
    
* **Batch Processing**: You can modify the script to batch process multiple PDF files by iterating through a directory of PDF documents.
    

<details data-node-type="hn-details-summary"><summary>Conclusion</summary><div data-type="detailsContent">Password-protecting PDF documents using Python is a straightforward process with the <code>PyPDF2</code> library. By following the steps in this guide, you can ensure that your sensitive documents are secure and accessible only to those with the appropriate password. This is particularly useful for businesses and individuals who need to share confidential information securely.</div></details>
