2014-09-30 131 views
4

我写了pdf破解并找到了受保护的pdf文件的密码。我想用Python编写一个程序,该程序可以在屏幕上显示该pdf文件而不需要密码。我使用PyPDF库。 我知道如何打开没有密码的文件,但无法弄清楚受保护的文件。任何想法?谢谢在python中打开一个受保护的pdf文件

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

回答

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"]) 
+1

“养NotImplementedError(” 只有算法代码1和2都支持 “)” 的错误。 – jamescampbell 2017-10-11 15:07:41

6

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

什么是解密.pdf在这里? – user5319825 2018-03-01 12:52:52

+1

这是'output_path',要写入的输出文件的名称。 – 2018-03-02 13:33:23