Reputation: 125
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
Upvotes: 6
Views: 30428
Reputation: 817
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.
import pikepdf
pdf = pikepdf.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()
Upvotes: 10
Reputation: 455
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)
Upvotes: 7
Reputation: 361
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.
Upvotes: 20
Reputation: 939
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
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)
Upvotes: 0
Reputation:
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/
Upvotes: 0