先前条件
需要安装两模块:fpdf
、PIL
pip install fpdf
pip install PIL
放码过来
from fpdf import FPDF
from PIL import Image
import osdef makePdf(pdfFileName, listPages):cover = Image.open(listPages[0])width, height = cover.sizepdf = 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