提示:点击关注作者,以获取其他的最新消息推送
前言
最近在处理文档时,需要将PDF文件拆分处理的图片重新合并到同个PDF文件中,因此重新编写一个批处理的Python程序,希望能够帮助到相同需要的同学。
提示:以下是本篇文章正文内容,下面案例可供参考
一、PIL、fpdf、tempfile、os是什么?
PIL 英文全称Python Imaging Library,是 Python 的图像处理库。
fpdf 是 Python PDF的库,可以用于创建PDF文件。
tempfile 是临时文件,用于批处理时找不到绝对路径而报错等问题。
os函数主要处理文件系统中目录及相关文件的操作,
二、步骤
1.引入库
代码如下:
from PIL import Image
from fpdf import FPDF
import os
import tempfile
2.读取数据
代码如下:
# 读取待整理图片的文件路径
img_paths = r'C:\Users\hp\Desktop\pdf\'
img_list = os.listdir(img_paths)
output_path = os.path.join(img_paths, "output.pdf")
os.llist(path),将path下的文件变为列表。
os.path.join(path, file),在path下创建file的绝对路径。
3.按文件名顺序整理
代码如下:
# 使用文件名的长度作为排序的关键
sorted_img_list = sorted(img_list, key=lambda x: len(x))
img_path_lists = [] #创建一个新的列表用于保存所有的图片路径
for img in sorted_img_list:
img_path = os.path.join(img_paths, img)
img_path_lists.append(img_path)
sorted(list, key=lambda x: len(x)), 将list 中的文件按照长度排序。
list.apped(a) ,在list中添加a
4.创建PDF并按整理好的顺序添加图片
代码如下:
def merge_images_to_pdf(image_paths, output_path):
pdf = FPDF() # 创建一个PDF对象
image_count = 0 # 用于计数
# 遍历图片路径列表
for image_path in image_paths:
with Image.open(image_path) as image:
image = image.convert('RGB') # 转换图片为RGB模式,因为fpdf库不支持其他模式
pdf.add_page()
# 使用临时文件的方式添加图片到PDF
temp_files = []
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_file: # 注意这里的suffix参数
image.save(temp_file.name) # 临时保存图像文件
pdf.image(temp_file.name, 0, 0, 210, 297) # 创建大小为:宽210,长297的 A4 页面
temp_files.append(temp_file.name)
image_count += 1
print(f"当前进度:第{image_count}张图片已添加到PDF中。")
# 保存PDF文件
pdf.output(output_path, "F")
print("图片转换为PDF成功!")
# 删除临时文件列表中的所有文件
for temp_file in temp_files:
os.remove(temp_file)
print("所有临时文件已删除。")
5.程序调用
代码如下:
merge_images_to_pdf(img_path_lists, output_path)
6.附整体代码
代码如下:
from PIL import Image
from fpdf import FPDF
import os
import tempfile
def merge_images_to_pdf(image_paths, output_path):
pdf = FPDF() # 创建一个PDF对象
image_count = 0 # 用于计数
# 遍历图片路径列表
for image_path in image_paths:
with Image.open(image_path) as image:
image = image.convert('RGB') # 转换图片为RGB模式,因为fpdf库不支持其他模式
pdf.add_page()
# 使用临时文件的方式添加图片到PDF
temp_files = []
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_file: # 注意这里的suffix参数
image.save(temp_file.name) # 临时保存图像文件
pdf.image(temp_file.name, 0, 0, 210, 297) # 创建大小为:宽210,长297的 A4 页面
temp_files.append(temp_file.name)
image_count += 1
print(f"当前进度:第{image_count}张图片已添加到PDF中。")
# 保存PDF文件
pdf.output(output_path, "F")
print("图片转换为PDF成功!")
# 删除临时文件列表中的所有文件
for temp_file in temp_files:
os.remove(temp_file)
print("所有临时文件已删除。")
if __name__ == "__main__":
# 读取待整理图片的文件路径
img_paths = r'C:\Users\hp\Desktop\pdf'
img_list = os.listdir(img_paths)
output_path = os.path.join(img_paths, "output.pdf")
# 使用文件名的长度作为排序的关键
sorted_img_list = sorted(img_list, key=lambda x: len(x))
img_path_lists = [] #创建一个新的列表用于保存所有的图片路径
for img in sorted_img_list:
img_path = os.path.join(img_paths, img)
img_path_lists.append(img_path)
merge_images_to_pdf(img_path_lists, output_path)
6.运行结果
总结
以上就是今天要讲的内容,本文使用到Python中的 PIL、fpdf、tempfile、os等函数。