Python Tool8:多张图片转PDF

Python Tool8:多张图片转PDF

安装库

pip install fpdf

Demo

''' Description: henggao_note version: v1.0.0 Date: 2022-07-07 17:25:50 LastEditors: henggao LastEditTime: 2022-07-07 21:06:21 ''' from importlib.resources import path from fpdf import FPDF from PIL import Image import os from fpdf import FPDF path1 = 'D:\\桌面\\temp3' path3 = 'D:\\桌面\\temp2\\' allpath=[] allname=[] allfilelist=os.listdir(path1) # 遍历该文件夹下的所有目录或者文件 for file in allfilelist: filepath=os.path.join(path1,file) filename = filepath.split('\\')[-1] print(filename) pdf = FPDF() pdf.set_auto_page_break(0) # 自动分页设为False imagelist = [i for i in os.listdir(filepath)] path2 = 'D:\桌面\\temp\\' + filename # print(path) # path = r(path) for image in sorted(imagelist): pdf.add_page() pdf.image(os.path.join(path2, image), w=190, h=150) # 指定宽高 name = filename + '.pdf' pdf.output(os.path.join(path3, name), "F")