Python 多个图片文件合并保存为PDF文件

2023-12-21 14:37:18
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/12/21 13:19
# @Author  : MengLi
# @Site    : 
# @File    : pic2pdf.py
# @Project : PythonCode
# @Software: PyCharm
# 照片转pdf,多图转多个pdf,多图转单个pdf,pdf合并
# imgToPdf.py

# coding = UTF-8
# coding = UTF-8


# 导入Python标准库
import os
from io import BytesIO
from PyPDF2 import PdfMerger
# 导入第三方库
from PIL import Image

# 防止中文乱码
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'

# 支持的图片文件格式
SUPPORT_SUFFIX = ["jpg", "jpeg", "png"]


def pic_to_pdf(image_bytes: bytes) -> bytes:
    """将单个图片转换为单张PDF

    :param image_bytes: 图片的bytes对象
    :return: PDF的bytes对象
    """
    # 将bytes对象转换为BytesIO对象
    image_bytes_io = BytesIO(image_bytes)
    # 从内存中读取图片
    image_object = Image.open(image_bytes_io)
    # 打开内存中的文件用于保存PDF
    with BytesIO() as result_bytes_io:
        # 将图片保存为单张PDF
        image_object.save(result_bytes_io, "PDF", resolution=100.0)
        # 获取内存中的文件
        data = result_bytes_io.getvalue()
    # 返回PDF的bytes对象
    return data


def batch_convert(image_path: str, pdf_path: str) -> None:
    """批量将图片转换为单张PDF

    :param image_path: 图片的文件夹
    :param pdf_path: PDF文件保存的文件夹
    """
    # 遍历文件夹下所有文件
    for root, dirs, files in os.walk(image_path, topdown=False):
        for name in files:
            if 'pdf' in name:
                continue
            # 提取文件的后缀名
            file_suffix = os.path.splitext(name)[-1].lstrip(".").lower()
            # 检测该文件格式是否受到支持
            if file_suffix not in SUPPORT_SUFFIX:
                continue
            # 拼接出图片文件的绝对路径
            source_file_path = os.path.join(root, name)
            # 拼接出PDF文件的绝对路径
            target_file_path = os.path.join(pdf_path, f"{os.path.splitext(name)[0]}.pdf")
            # 将图片文件转换为PDF文件
            with open(source_file_path, "rb") as source:
                with open(target_file_path, "wb") as target:
                    target.write(pic_to_pdf(source.read()))


# 合并pdf
def mergePdf(i, pdf_list):
    # 将所有 PDF 合并成一个文件
    merger = PdfMerger()
    for file_path in pdf_list:
        merger.append(str(file_path))
    merger.write("output%s.pdf" % i)


if __name__ == '__main__':
    pics_path = r"C:\Users\15200\Desktop\pic2pdf"
    pdf_path = r"C:\Users\15200\Desktop\pic2pdf"
    batch_convert(pics_path, pdf_path)
    pdf_list = []
    for i in os.listdir(pdf_path):
        if "pdf" in i:
            pdf_list.append(os.path.join(pdf_path, i ))
    mergePdf('_all', pdf_list)

供大家白嫖?

文章来源:https://blog.csdn.net/linxizi0622/article/details/135129466
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。