我一直在用Python创建一个程序,用于将2个PDF文件合并为一个文件。下面是代码:-
import os
from PyPDF2 import PdfFileMerger
source_dir = os.getcwd()
merger = PdfFileMerger()
for item in os.listdir(source_dir):
if item.endswith('pdf'):
merger.append(item)
merger.write('completed_file.pdf')
merger.close()
运行代码时遇到以下错误:-
"F:\Python folder\Pdf_Merger\venv\Scripts\python.exe" "F:/Python folder/Pdf_Merger/main.py"
Traceback (most recent call last):
File "F:\Python folder\Pdf_Merger\venv\lib\site-packages\PyPDF2\pdf.py", line 1147, in getNumPages
self.decrypt('')
File "F:\Python folder\Pdf_Merger\venv\lib\site-packages\PyPDF2\pdf.py", line 1987, in decrypt
return self._decrypt(password)
File "F:\Python folder\Pdf_Merger\venv\lib\site-packages\PyPDF2\pdf.py", line 1996, in _decrypt
raise NotImplementedError("only algorithm code 1 and 2 are supported")
NotImplementedError: only algorithm code 1 and 2 are supported
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "F:\Python folder\Pdf_Merger\main.py", line 10, in <module>
merger.append(item)
File "F:\Python folder\Pdf_Merger\venv\lib\site-packages\PyPDF2\merger.py", line 203, in append
self.merge(len(self.pages), fileobj, bookmark, pages, import_bookmarks)
File "F:\Python folder\Pdf_Merger\venv\lib\site-packages\PyPDF2\merger.py", line 139, in merge
pages = (0, pdfr.getNumPages())
File "F:\Python folder\Pdf_Merger\venv\lib\site-packages\PyPDF2\pdf.py", line 1150, in getNumPages
raise utils.PdfReadError("File has not been decrypted")
PyPDF2.utils.PdfReadError: File has not been decrypted
Process finished with exit code 1
1条答案
按热度按时间mkshixfv1#
只要更新到
PyPDF2==2.12.1
或者更好的pypdf>=3.5.0
就可以了。我是这两个版本的维护者。我们在2022年对pypdf做了很多改进,特别增加了几个加解密算法。