我在学习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

img


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