使用python将多张图片合成PDF

先安装 pillow 介绍

pip install pillow

将多张图片合成PDF

复制代码
from PIL import Image
import os

# 防止字符串乱码
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'

def pic2pdf(img_path, pdf_path):
    file_list = os.listdir(img_path)
    sources = []
    jpg_files = []
    for file in file_list:
        if 'png' in file or 'jpg' in file:
            jpg_files.append(file)
    
    jpg_files.sort(key=lambda x: int(x.split('.')[0]))
    output = Image.open(img_path + jpg_files[0])
    jpg_files.pop(0)
    for file in jpg_files:
        jpg_file = Image.open(img_path + file)
        if jpg_file.mode == "RGB":
            jpg_file = jpg_file.convert("RGB")
        sources.append(jpg_file)
    output.save(pdf_path, "pdf", save_all=True, append_images=sources)


# 待转换图像路径
img_path = "../data/materials/ABC/"
# 转换后的pdf
pdf_path = "../data/materials/ABC.pdf"
pic2pdf(img_path=img_path, pdf_path=pdf_path)
复制代码

 

posted @   慕尘  阅读(747)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2019-09-20 Spring boot启动成功后输出提示
2019-09-20 Properties的有序读写
2019-09-20 javascript中var、let、const的区别
点击右上角即可分享
微信分享提示