Python自动化办公之图片转PDF的实现
作者:Python 发布时间:2023-07-24 21:29:07
标签:Python,图片,PDF
安装的方式很常规,直接使用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格式
下面是实现的示例代码
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
0
投稿
猜你喜欢
- 如何在生产上部署Django?Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式。uwsgi介绍uW
- 最近在一个python工具中需要实现串口自动触发工作的功能,之前只在winform上面实现,今天使用python试试。这里简单记一下:首先用
- 代码如下:< % '功能:显示数据库中表名、字段名、字段内容 '原创:wangsdong
- 由于ajax在跨域的访问上有问题,目前最好的方法是做代理.写了个代理程序和心得为了做ajax的代理,研究了下服务器端的xmlhttp并和客户
- 我就废话不多说了,直接上代码吧!a=[[1,2,3],[4,5][6,7]["a","b""
- <!-- #include file="../conn.asp" --&
- 利用python pyheatmap包绘制热力图,供大家参考,具体内容如下import matplotlib.pyplot as pltfr
- 这里的Counter是指collections中的Counter,通过Counter可以实现字典的创建以及字典key出现频次的统计。然而,使
- 工作中每天需要收集部门内的FR文件,发送给外部部门的同事帮忙上传,这么发了有大半年,昨天亮光一闪,为什么不做成自动化呢,于是用python实
- 一、步骤要从 JPEG 图像中获取经纬度信息,可以使用 Python 的 PIL(Python Imaging Library)库。以下是一
- 使用groupby([ ]).size()统计的结果,值相同的字段值会不显示如上图所示,第一个空着的行是982499 7 3388 1,因为
- 阅读Chapter 1 清单Chapter 2 标题总览:不但所有网页都需要有标题,而且如果标记正确的话,他们能为网页设计和易用性
- 茎叶图from itertools import groupbynums2=[225, 232,232,245,235,245,270,22
- Web Forms 2.0 是一个很有意思的东东,是 HTML 5 的组成部分。它的目标是提升表单的使用性 (usability),基本上就
- 在一个文件的末尾追加数据是很常用的。在使用过程中应该都比较熟悉不会出现什么错误。但是往一个文件头部插入数据可能或多或少会碰到一些问题。看似正
- 1. 连接对象操作数据库之前,首先要建立数据库连接。有下面几个方法进行连接。>>>import cx_Oracle>
- 利用Keras构建完普通BP神经网络后,还要会构建CNNKeras中构建CNN的重要函数1、Conv2DConv2D用于在CNN中构建卷积层
- pandas删除部分数据后重新索引在使用pandas时,由于隔行读取删除了部分数据,导致删除数据后的索引不连续:原数据删除部分数据后在绑定p
- 导读只需要添加几行代码,就可以得到更快速,更省显存的PyTorch模型。你知道吗,在1986年Geoffrey Hinton就在Nature
- 本文介绍了python OpenCV学习笔记实现二维直方图,分享给大家,具体如下:官方文档 – https://docs.opencv.or