4

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)
rammbb
  • 41
  • 1
  • 1
  • 4
  • 1
    I'd start with [installing PyCryptodome](https://pypi.org/project/pycryptodome/). – AKX Sep 13 '22 at 10:01
  • Please provide enough code so others can better understand or reproduce the problem. – Community Sep 13 '22 at 10:05
  • I do have pycryptodome installed. I get an error here: with open(filename, "rb") as in_file: input_pdf = PdfFileReader(in_file) – rammbb Sep 13 '22 at 10:34

2 Answers2

8

I had the same problem.

You just need to install PyCryptodome package.

For example:

pip install pycryptodome==3.15.0
Anton Belsky
  • 159
  • 9
0

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)
General Grievance
  • 4,555
  • 31
  • 31
  • 45
sogu
  • 2,738
  • 5
  • 31
  • 90
  • 1
    I get the same error using this code. If I hardcode name of pdf it works as supposed to. Problem occurs in some reasons while looping over excel file read by pandas :/ – rammbb Sep 13 '22 at 11:13
  • @rammbb I have updated my answer B.) ```! pip install pikepdf``` – sogu Sep 13 '22 at 11:19
  • It doesn't answer my question. I don't want to read it but SET a password. – rammbb Sep 13 '22 at 11:32
  • 1
    @rammbb yep but the password settings seems all right, but as you mention the reading causes the error. – sogu Sep 13 '22 at 11:35