在python中打开受保护的pdf文件

时间:2014-09-30 21:00:16

标签: python pdf passwords

我写了一个pdf破解并找到了受保护的pdf文件的密码。我想用Python编写一个程序,可以在没有密码的情况下在屏幕上显示该pdf文件。我使用PyPDF库。 我知道如何在没有密码的情况下打开文件,但无法找出受保护的文件。任何想法?感谢

filePath = raw_input()
password = 'abc'
if sys.platform.startswith('linux'):
       subprocess.call(["xdg-open", filePath])

3 个答案:

答案 0 :(得分:10)

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

答案 1 :(得分:2)

您现在应改为使用pikepdf库:

import pikepdf

with pikepdf.open("input.pdf", password="abc") as pdf:
    num_pages = len(pdf.pages)
    print("Total pages:", num_pages)

PyPDF2不支持许多加密算法,pikepdf似乎可以解决这些问题,它支持大多数受密码保护的方法,并且已记录并积极维护。

答案 2 :(得分:1)

我有这个问题的答案。基本上,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"])