m0_66452591 2022-04-06 11:32 采纳率: 100%
浏览 261
已结题

我在学习PyPDF2,出现了如下问题PyPDF2.utils.PdfReadError: File has not been decrypted

问题遇到的现象和发生背景

我在学习《Python编程快速上手--让繁琐工作自动化(第2版)》这一本书,在15.2项目:从多个PDF中合并选择的页面,过程中出现了错误。我的目的是将该文件夹下所有PDF文档合并到一个文件。

问题相关代码,请勿粘贴截图
#! python3
# combinePdfs.py - Combines all the PDFs in the current working directory into
# a single PDF.

import PyPDF2, os

# Get all the PDF filenames.
pdfFiles = []
for filename in os.listdir('.'):
    if filename.endswith('.pdf'):
        pdfFiles.append(filename)
pdfFiles.sort()

pdfWriter = PyPDF2.PdfFileWriter()

# Loop through all the PDF files.
for filename in pdfFiles:
    pdfFileObj = open(filename, 'rb')
    pdfReader = PyPDF2.PdfFileReader(pdfFileObj)

    # Loop through all the pages (except the first) and add them.
    for pageNum in range(1, pdfReader.numPages):
        pageObj = pdfReader.getPage(pageNum)
        pdfWriter.addPage(pageObj)

# Save the resulting PDF to a file.
pdfOutput = open('allminutes.pdf', 'wb')
pdfWriter.write(pdfOutput)
pdfOutput.close()


运行结果及报错内容

img

我的解答思路和尝试过的方法

我通过查阅资料发现出现这类错误的原因可能是其中几个PDF文件设有密码,从而导致文件读取失败。

我想要达到的结果

我不知道该如何调整自己的代码,我想让各位直接对我代码进行修改,以便达到合并文档的目的。
以下是我的PDF文件名称及密码:
encrypted.pdf 密码:rosebud
encryptedminutes.pdf 密码:swordfish

  • 写回答

1条回答 默认 最新

  • 陈年椰子 2022-04-12 22:03
    关注

    img


    加一句解密, 把a123 改成你对应的密码即可。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 6月1日
  • 已采纳回答 5月24日
  • 创建了问题 4月6日

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败