PyPDF2 error "PyCryptodome is required for AES algorithm"

20.7k Views Asked by At

I've got hundreds on PDFs I need to set password. I tried to use pyPDF2 to do that but I got an error: "DependencyError: PyCryptodome is required for AES algorithm".

I've tried to google any other module like pikepdf but I found only how to crack the password using it and not to actually set password.

Any ideas how to deal with it? I get an error on that line: "input_pdf = PdfFileReader(in_file)"

file = directory + '\\passwords.xlsx'  

df = pd.read_excel(file)
df['PDF'] = df.iloc[:,[0]] + '.pdf'

df = df.to_dict('records')
for i in df:
    filename = i['PDF']
    password = i['Password']

    with open(filename, "rb") as in_file:
        input_pdf = PdfFileReader(in_file)

    output_pdf = PdfFileWriter()
    output_pdf.appendPagesFromReader(input_pdf)
    output_pdf.encrypt(password)

    with open(filename, "wb") as out_file:
        output_pdf.write(out_file)
2

There are 2 best solutions below

4
sogu On

A.) Great way to do it:

https://roytuts.com/how-to-encrypt-pdf-as-password-protected-file-in-python/

import PyPDF2

#pdf_in_file = open("simple.pdf",'rb')
pdf_in_file = open("gre_research_validity_data.pdf",'rb')

inputpdf = PyPDF2.PdfFileReader(pdf_in_file)
pages_no = inputpdf.numPages
output = PyPDF2.PdfFileWriter()

for i in range(pages_no):
    inputpdf = PyPDF2.PdfFileReader(pdf_in_file)
    
    output.addPage(inputpdf.getPage(i))
    output.encrypt('password')

    #with open("simple_password_protected.pdf", "wb") as outputStream:
    with open("gre_research_validity_data_password_protected.pdf", "wb") as outputStream:
        output.write(outputStream)

pdf_in_file.close()

B.) If you want to fix your own bug:

solution for similar error message but during counting pages - Not able to find number of pages of PDF using Python 3.X: DependencyError: PyCryptodome is required for AES algorithm

ORIGINAL CODE

! pip install PyPDF2
! pip install pycryptodome
from PyPDF2 import PdfFileReader
from Crypto.Cipher import AES

if PdfFileReader('Media Downloaded Files/spk-10-3144 bro.pdf').isEncrypted:
    print('This file is encrypted.')
else:
    print(PdfFileReader('Media Downloaded Files/spk-10-3144-bro.pdf').numPages)

FIX

! pip install pikepdf 
from pikepdf import Pdf  
pdf = Pdf.open('Media Downloaded Files/spk-10-3144-bro.pdf') 
len(pdf.pages)
0
Anton Belsky On

I had the same problem.

You just need to install PyCryptodome package.

For example:

pip install pycryptodome==3.15.0