题目:
将若干图片合并成一个 PDF 文件
视频教程:
代码1:
import osfrom pathlib import Pathfrom fpdf import FPDFsource_dir = Path('./storage')print('目录 %s 下有文件:' % source_dir)images = list(source_dir.glob('*.jpg'))for name in images:print(name)result_dir = Path('./result')if not result_dir.exists():result_dir.mkdir(parents=True)pdf = FPDF(format='A4')for image in images:pdf.add_page()pdf.image(str(image), x=0, y=0, w=210, h=297)dest = os.path.join(result_dir, "book.pdf")pdf.output(dest)print('输出到:', dest)
代码2:
【Python入门100题之023:图片合并成PDF】import osfrom pathlib import Pathfrom fpdf import FPDFsource_dir = Path('./storage')print('目录 %s 下有文件:' % source_dir)images = list(source_dir.glob('*.jpg'))images= list(source_dir.glob('*.png'))for name in images:print(name)result_dir = Path('./result')if not result_dir.exists():result_dir.mkdir(parents=True)pdf = FPDF()for image in images:pdf.add_page()pdf.image(str(image), x=0, y=0, w=210, h=297)dest = os.path.join(result_dir, "book.pdf")pdf.output(dest)print('输出到:', dest)