我想将目录中的所有PDF与PyPDF2合并。
我尝试了pypdf Merging multiple pdf files into one pdf中的代码
from PyPDF2 import PdfFileMerger, PdfFileReader
merger = PdfFileMerger()
for filename in os.listdir():
merger.append(PdfFileReader(file(filename, 'rb')))
merger.write('Result.pdf')
我出错了!
NameError: name 'file' is not defined
答案 0 :(得分:0)
使用with
块
for filename in os.listdir():
with open(filename, 'rb') as source:
tmp = PdfFileReader(source)
merger.append(tmp)