我写了一个pdf破解程序,找到了受保护的pdf文件的密码。我想用Python语言写一个程序,不用密码就能在屏幕上显示这个pdf文件。我使用PyPDF库。我知道如何在没有密码的情况下打开文件,但不能理解受保护的one.Any的想法?谢谢
filePath = raw_input()
password = 'abc'
if sys.platform.startswith('linux'):
subprocess.call(["xdg-open", filePath])
发布于 2017-01-18 15:56:38
KL84显示的方法基本上可以工作,但代码不正确(它为每个页面编写输出文件)。下面是一个经过清理的版本:
https://gist.github.com/bzamecnik/1abb64affb21322256f1c4ebbb59a364
# Decrypt password-protected PDF in Python.
#
# Requirements:
# pip install PyPDF2
from PyPDF2 import PdfFileReader, PdfFileWriter
def decrypt_pdf(input_path, output_path, password):
with open(input_path, 'rb') as input_file, \
open(output_path, 'wb') as output_file:
reader = PdfFileReader(input_file)
reader.decrypt(password)
writer = PdfFileWriter()
for i in range(reader.getNumPages()):
writer.addPage(reader.getPage(i))
writer.write(output_file)
if __name__ == '__main__':
# example usage:
decrypt_pdf('encrypted.pdf', 'decrypted.pdf', 'secret_password')
发布于 2020-05-17 14:07:48
现在你应该改用pikepdf库:
import pikepdf
with pikepdf.open("input.pdf", password="abc") as pdf:
num_pages = len(pdf.pages)
print("Total pages:", num_pages)
PyPDF2
不支持许多加密算法,pikepdf
似乎解决了这些问题,它支持大多数密码保护方法,并且还记录并积极维护。
发布于 2014-10-24 05:22:29
我有这个问题的答案。基本上,为了实现这个想法,需要安装和使用PyPDF2库。
#When you have the password = abc you have to call the function decrypt in PyPDF to decrypt the pdf file
filePath = raw_input("Enter pdf file path: ")
f = PdfFileReader(file(filePath, "rb"))
output = PdfFileWriter()
f.decrypt ('abc')
# Copy the pages in the encrypted pdf to unencrypted pdf with name noPassPDF.pdf
for pageNumber in range (0, f.getNumPages()):
output.addPage(f.getPage(pageNumber))
# write "output" to noPassPDF.pdf
outputStream = file("noPassPDF.pdf", "wb")
output.write(outputStream)
outputStream.close()
#Open the file now
if sys.platform.startswith('darwin'):#open in MAC OX
subprocess.call(["open", "noPassPDF.pdf"])
https://stackoverflow.com/questions/26130032
复制相似问题