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)
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()
solution for similar error message but during counting pages - https://dailydevsblog.com/troubleshoot/solved-not-able-to-find-number-of-pages-of-pdf-using-python-3-x-dependencyerror-pycryptodome-is-required-for-aes-algorithm-221411/
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.