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

Open a protected pdf file in python(在python中打开一个受保护的pdf文件)
本文介绍了在python中打开一个受保护的pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I write a pdf cracking and found the password of the protected pdf file. I want to write a program in Python that can display that pdf file on the screen without password.I use the PyPDF library. I know how to open a file without the password, but can't figure out the protected one.Any idea? Thanks

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

推荐答案

KL84给出的方法基本可以,但是代码不正确(每页都写输出文件).清理后的版本在这里:

The approach shown by KL84 basically works, but the code is not correct (it writes the output file for each page). A cleaned up version is here:

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

这篇关于在python中打开一个受保护的pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)