以下功能已经验证通过,欢迎使用不客气。
import img2pdf
import os
def convert_images_to_pdf(image_folder_path, output_pdf_path):
# 确保输入路径是文件夹
if not os.path.isdir(image_folder_path):
raise ValueError("The input path must be a directory.")
# 获取所有图片文件
image_files = [os.path.join(image_folder_path, f) for f in os.listdir(image_folder_path) if
f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))]
# 排序文件名以确保顺序
image_files.sort()
# 读取图片内容
with open(output_pdf_path, "wb") as pdf_file:
pdf_data = img2pdf.convert(image_files)
pdf_file.write(pdf_data)
# 使用示例
convert_images_to_pdf('D:\\Desktop\\', 'D:\\Desktop\\6-7.pdf')
#前边是目录,后面参数放需要生成的文件名。