Python自动化办公之图片转PDF的实现

作者:Python 时间:2023-07-24 21:29:07 

安装的方式很常规,直接使用pip安装就行了。

pip install fpdf

将需要使用的三方模块导入进来

from fpdf import FPDF  # PDF文档对象操作库
import os  # 文件路径操作库

初始化PDF文档对象

PDF = FPDF()

关闭自动分页

PDF.set_auto_page_break(0)

设置需要转换的批量图片路径

path = r'C:/imgs'

遍历图片到数组

images = [i for i in os.listdir(path)]

设置多少张图片在PDF中占一页

NUM = int(input('参数设置: 请输入多少张图片占用一页: \n'))

设置图片的宽度和高度

width = int(input('参数设置: 请输入每张图片的宽度: \n'))
height = int(input('参数设置: 请输入每张图片的高度: \n'))

遍历图片并向文档中添加图片

for index, image in enumerate(images):
   if index == 0:
       PDF.add_page()
   elif index % NUM == 0:
       PDF.add_page()
   PDF.image(os.path.join(path, image), w=width, h=height)

保存PDF文档

PDF.output(os.path.join(path, "图片文档.pdf"), "F")

print('图片到PDF转换完成!')

实现效果图

Python自动化办公之图片转PDF的实现

补充

当然Python还能实现多张图片合并转PDF格式

下面是实现的示例代码


from PIL import Image
import os
import img2pdf

flag = False
while not flag:
   dirname = input("请输入图片文件夹所在路径(例如d:/wlzcool):")
   flag = os.path.exists(dirname)
   if not flag:
       print("图片文件夹所在路径不存在!")
saveflag = False
while not saveflag:
   savedirname = input("请输入目标图片文件夹所在路径(例如d:/wlzcool2):")
   saveflag = os.path.exists(savedirname)
   if not saveflag:
       print("图片文件夹所在路径不存在!")
       automakedir = input("是否自动创建对应文件夹?(是Y/否N):")
       if automakedir.strip().upper() == "Y":
           os.makedirs(savedirname)
           saveflag = True
files = os.listdir(dirname)
reductionFactor = int(input("请输入长宽压缩比(例如3):"))
if reductionFactor <= 0:
   reductionFactor = 3
isConvertBlack = input("是否输出黑白版本?(是Y/否N):").strip().upper() == "Y"
for fname in files:
   if not fname.endswith(".jpg"):
       continue
   path = os.path.join(dirname, fname)
   savePath = os.path.join(savedirname, fname)
   if os.path.isdir(path):
       continue
   img = Image.open(path)    
   if img.size[0] > img.size[1]:
       im_rotate = img.rotate(90, expand=True)
       size = (int(im_rotate.size[0] / reductionFactor), int(im_rotate.size[1] / reductionFactor))
       im_rotate = im_rotate.resize(size)
       if isConvertBlack:
           im_rotate = im_rotate.convert("L")
       im_rotate.save(savePath, quality=95)
   else:
       size = (int(img.size[0] / reductionFactor), int(img.size[1] / reductionFactor))
       img = img.resize(size)
       if isConvertBlack:
           img = img.convert("L")
       img.save(savePath, quality=95)
filename = input("请输入输出文件名(例如:第一章):")
with open(filename + ".pdf", "wb") as f:
   imgs = []
   files = os.listdir(savedirname)
   for fname in files:
       if not fname.endswith(".jpg"):
           continue
       path = os.path.join(savedirname, fname)
       if os.path.isdir(path):
           continue
       imgs.append(path)
   f.write(img2pdf.convert(imgs))

来源:https://www.cnblogs.com/lwsbc/p/16142099.html

标签:Python,图片,PDF
0
投稿

猜你喜欢

  • Python中__init__.py文件的作用详解

    2021-12-22 13:04:05
  • Python 异步之推导式示例详解

    2021-05-16 17:31:08
  • Python基于opencv实现的人脸识别(适合初学者)

    2021-10-13 19:39:29
  • mysql通过查看跟踪日志跟踪执行的sql语句

    2024-01-28 00:56:10
  • 微信小程序新手教程之启动页的重要性

    2023-07-02 05:26:00
  • php动态生成函数示例

    2024-05-02 17:19:00
  • 妙用Dreamweaver MX共享Word XP文件

    2010-09-05 21:17:00
  • Python实现简单http服务器

    2022-02-04 03:55:01
  • SQL 合并多行记录的方法总汇

    2024-01-22 08:42:56
  • Python 的矩阵传播机制Broadcasting和矩阵运算

    2021-07-28 01:03:34
  • 仅用50行Python代码实现一个简单的代理服务器

    2022-11-26 14:44:57
  • Python+django实现文件上传

    2022-08-31 20:52:22
  • 解决mysql不能插入中文Incorrect string value

    2009-07-30 09:02:00
  • 使用 XML 文件记录操作日志

    2008-09-05 17:13:00
  • python3 使用ssh隧道连接mysql的操作

    2023-09-01 19:15:11
  • Python的互斥锁与信号量详解

    2021-12-24 15:29:34
  • 用户体验的误解

    2008-07-15 12:31:00
  • python实现控制电脑鼠标和键盘,登录QQ的方法示例

    2023-11-19 12:10:26
  • 详解微信小程序图片地扯转base64解决方案

    2024-06-18 05:52:55
  • 将一个图片以二进制值的形式存入Xml文件中

    2008-09-04 11:24:00
  • asp之家 网络编程 m.aspxhome.com