Encrypt PDFs in python

Question:

Is there a possible and way to encrypt PDF-Files in python?
One possibility is to zip the PDFs but is there another ?
Thanks for your help
regards
Felix

Asked By: Felix Becker

||

Answers:

You can use pypdf:

from pypdf import PdfReader, PdfWriter

reader = PdfReader("example.pdf")

writer = PdfWriter()
writer.append_pages_from_reader(reader)
writer.encrypt("password")

with open("output.pdf", "wb") as out_file:
    writer.write(out_file)

For more information, check out the PdfWriter docs.

Answered By: J08nY

You can use pypdf

import pypdf
# Create reader and writer object
reader = pypdf.PdfReader("input.pdf")
writer = pypdf.PdfWriter()
# Add all pages to writer (accepted answer results into blank pages)
for page in reader.pages:
    writer.add_page(page)
# Encrypt with your password
writer.encrypt('password')
# Write it to an output file. (you can delete unencrypted version now)
with open('encrypted_output.pdf', 'wb') as resultPdf:
    writer.write(resultPdf)
Answered By: Siddhesh Suthar

I would highly recommend the pyAesCrypt module.
It is based on the Cryptography module which is written partly in C.
The module is quite fast, especially in high spec computers.
You can expect a 12 second encryption of a 3 Gb file on higher end computers, so It really is fast though not the best one.

One liner for encryptions and Decryptions are:

import pyAesCrypt

Encrypting:

pyAesCrypt.encryptFile(inputfile, outputfile, password, bufferSize)

Decrypting:

pyAesCrypt.decryptFile(inputfile, outputfile, password, bufferSize)

Since this is not the full explanation I would recommend to fully read the documentation as It is not really long.
You can find it here: https://pypi.org/project/pyAesCrypt/

Answered By: user11269100

PikePdf which is python’s adaptation of QPDF, is by far the better option. This is especially helpful if you have a file that has text in languages other than English.

from pikepdf import Pdf
pdf = Pdf.open(path/to/file)    
pdf.save('output_filename.pdf', encryption=pikepdf.Encryption(owner=password, user=password, R=4)) 
# you can change the R from 4 to 6 for 256 aes encryption
pdf.close()
Answered By: Asad Rauf

Another option is Aspose.PDF Cloud SDK for Python, it is a rest API solution. You can use cloud storage of your choice from Amazon S3, DropBox, Google Drive Storage, Google Cloud Storage, Windows Azure Storage, FTP Storage and Aspose Cloud Storage.

The cryptoAlgorithm takes the follwing possible values

  • RC4x40: RC4 with key length 40
  • RC4x128: RC4 with key length 128
  • AESx128: AES with key length 128
  • AESx256: AES with key length 256
import os
import base64
import asposepdfcloud
from asposepdfcloud.apis.pdf_api import PdfApi
from shutil import copyfile

# Get Client key and Client ID from https://cloud.aspose.com
pdf_api_client = asposepdfcloud.api_client.ApiClient(
    app_key='xxxxxxxxxxxxxxxxxxxxxxxxxx',
    app_sid='xxxxxx-xxxx-xxxxx-xxxx-xxxxxxxxxxx')

pdf_api = PdfApi(pdf_api_client)
temp_folder="Temp"

#upload PDF file to storage
data_file = "C:/Temp/02_pages.pdf"
remote_name= "02_pages.pdf"
pdf_api.upload_file(remote_name,data_file)

out_path = "EncryptedPDF.pdf"

user_password_encoded = base64.b64encode(b'user $^Password!&')
owner_password_encoded = base64.b64encode(b'owner//? $12^Password!&')

#Encrypte PDF document
response = pdf_api.put_encrypt_document(temp_folder + '/' + out_path, user_password_encoded, owner_password_encoded, asposepdfcloud.models.CryptoAlgorithm.AESX128, file = remote_name)

#download PDF file from storage
response_download = pdf_api.download_file(temp_folder + '/' + out_path)
copyfile(response_download, 'C:/Temp/' + out_path)
print(response)

Answered By: Tilal Ahmad

You can also use PyPDF2 with this project.

For example, put the PDF_Lock.py file into your project folder.
Then you can use:

import PDF_Lock

and when you want protect a PDF file use:

PDF_Lock.lock(YourPDFFilePath, YourProtectedPDFFilePath, Password)
Answered By: Giacomo De Florio
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.