2014-09-30 2 views
4

나는 pdf 파일을 열어서 보호 된 pdf 파일의 비밀번호를 발견했다. 암호없이 화면에 pdf 파일을 표시 할 수있는 Python 프로그램을 작성하고 싶습니다. 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

"(단 알고리즘 코드 1과 2는 지원") "오류를 NotImplementedError 인상". – jamescampbell

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

여기에 해독 된 내용은 무엇입니까? – user5319825

+1

출력 파일의 이름 인'output_path'입니다. –