实用代码Python(二):使用PyPDF2融合多个PDF文件

通用代码

将要融合的PDF放到同一目录下

from PyPDF2 import PdfFileMerger
import os
files = os.listdir()

pdfs = []

for file in files:
	if file[-4:] == ".pdf":
		pdfs.extend([file])


merger = PdfFileMerger()

for pdf in pdfs:
    merger.append(open(pdf, 'rb'))

with open('newfile.pdf', 'wb') as fout:
    merger.write(fout)
	

案例一代码

from PyPDF2 import PdfFileMerger

pdfs = ['0.title.pdf', '1.overview.pdf', '2.quickstart.pdf', '3.datastructure.pdf','4.templet.pdf', '5.moretemplet.pdf', '6.visualization.pdf', '7.details.pdf']

merger = PdfFileMerger()

for pdf in pdfs:
    merger.append(open(pdf, 'rb'))

with open('geatpy教程.pdf', 'wb') as fout:
    merger.write(fout)


案例二代码

from PyPDF2 import PdfFileMerger

pdfs = ['序言.pdf', '第一章:概述.pdf', '第二章:编码.pdf', '第三章:适应度.pdf','第四章:选择.pdf', '第五章:重组.pdf', '第六章:变异.pdf', '第七章:重插入.pdf','第八章:多目标优化.pdf']

merger = PdfFileMerger()

for pdf in pdfs:
    merger.append(open(pdf, 'rb'))

with open('进化算法介绍.pdf', 'wb') as fout:
    merger.write(fout)


你可能感兴趣的:(Python实用代码)