首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在python中打开一个受保护的pdf文件

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

Stack Overflow用户
提问于 2014-10-01 05:00:16
回答 4查看 22.6K关注 0票数 7

我写了一个pdf破解程序,找到了受保护的pdf文件的密码。我想用Python语言写一个程序,不用密码就能在屏幕上显示这个pdf文件。我使用PyPDF库。我知道如何在没有密码的情况下打开文件,但不能理解受保护的one.Any的想法?谢谢

代码语言:javascript
运行
复制
filePath = raw_input()
password = 'abc'
if sys.platform.startswith('linux'):
       subprocess.call(["xdg-open", filePath])
EN

回答 4

Stack Overflow用户

发布于 2017-01-18 15:56:38

KL84显示的方法基本上可以工作,但代码不正确(它为每个页面编写输出文件)。下面是一个经过清理的版本:

https://gist.github.com/bzamecnik/1abb64affb21322256f1c4ebbb59a364

代码语言:javascript
运行
复制
# 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')
票数 14
EN

Stack Overflow用户

发布于 2020-05-17 14:07:48

现在你应该改用pikepdf库:

代码语言:javascript
运行
复制
import pikepdf

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

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

票数 5
EN

Stack Overflow用户

发布于 2014-10-24 05:22:29

我有这个问题的答案。基本上,为了实现这个想法,需要安装和使用PyPDF2库。

代码语言:javascript
运行
复制
#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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26130032

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档