繁体   English   中英

PyPDF2 错误“AES 算法需要 PyCryptodome”

[英]PyPDF2 error "PyCryptodome is required for AES algorithm"

我有数百个需要设置密码的 PDF。 我尝试使用 pyPDF2 来执行此操作,但出现错误:“DependencyError:AES 算法需要 PyCryptodome”。

我试图用谷歌搜索任何其他模块,如 pikepdf,但我只发现如何使用它来破解密码,而不是实际设置密码。

任何想法如何处理它? 我在该行收到错误:“ 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)

A.) 这样做的好方法:

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.) 如果您想修复自己的错误:

类似错误消息的解决方案,但在计数页面期间 - 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/

原始代码

! 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)

使固定

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM