用Python将多张图片合并成一PDF文件

懵懂的女人 提交于 2020-02-27 12:42:47

先前条件

需要安装两模块:fpdfPIL

  • pip install fpdf
  • pip install PIL

放码过来

from fpdf import FPDF
from PIL import Image
import os

def makePdf(pdfFileName, listPages):

	cover = Image.open(listPages[0])
	width, height = cover.size

	pdf = FPDF(unit = "pt", format = [width, height])

	for page in listPages:
		pdf.add_page()
		pdf.image(page, 0, 0)

	pdf.output(pdfFileName, "F")

makePdf("result.pdf", [imgFileName for imgFileName in os.listdir('.') \
					   if imgFileName.endswith("png")])

参考文献

Create PDF from a list of images

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!