網站首頁 編程語言 正文
安裝的方式很常規,直接使用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
相關推薦
- 2022-05-03 在Django中動態地過濾查詢集的實現_python
- 2023-11-16 【電腦Windows日常】解決Windows11 無法顯示office圖標的問題
- 2022-10-11 Linux中數據傳輸命令scp/rsync
- 2022-08-06 python練習之循環控制語句?break?與?continue_python
- 2022-06-01 Python?中enum的使用方法總結_python
- 2023-05-14 Python稀疏矩陣scipy.sparse包使用詳解_python
- 2022-10-16 python讀取Windows注冊表的示例代碼_python
- 2022-12-15 Qt開發之使用socket實現遠程控制_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支