Python自動(dòng)化辦公之圖片轉(zhuǎn)PDF的實(shí)現(xiàn)
安裝的方式很常規(guī),直接使用pip安裝就行了。
pip install fpdf
將需要使用的三方模塊導(dǎo)入進(jìn)來
from fpdf import FPDF # PDF文檔對(duì)象操作庫(kù) import os # 文件路徑操作庫(kù)
初始化PDF文檔對(duì)象
PDF = FPDF()
關(guān)閉自動(dòng)分頁(yè)
PDF.set_auto_page_break(0)
設(shè)置需要轉(zhuǎn)換的批量圖片路徑
path = r'C:/imgs'
遍歷圖片到數(shù)組
images = [i for i in os.listdir(path)]
設(shè)置多少?gòu)垐D片在PDF中占一頁(yè)
NUM = int(input('參數(shù)設(shè)置: 請(qǐng)輸入多少?gòu)垐D片占用一頁(yè): \n'))
設(shè)置圖片的寬度和高度
width = int(input('參數(shù)設(shè)置: 請(qǐng)輸入每張圖片的寬度: \n'))
height = int(input('參數(shù)設(shè)置: 請(qǐng)輸入每張圖片的高度: \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轉(zhuǎn)換完成!')實(shí)現(xiàn)效果圖

補(bǔ)充
當(dāng)然Python還能實(shí)現(xiàn)多張圖片合并轉(zhuǎn)PDF格式
下面是實(shí)現(xiàn)的示例代碼
from PIL import Image
import os
import img2pdf
flag = False
while not flag:
dirname = input("請(qǐng)輸入圖片文件夾所在路徑(例如d:/wlzcool):")
flag = os.path.exists(dirname)
if not flag:
print("圖片文件夾所在路徑不存在!")
saveflag = False
while not saveflag:
savedirname = input("請(qǐng)輸入目標(biāo)圖片文件夾所在路徑(例如d:/wlzcool2):")
saveflag = os.path.exists(savedirname)
if not saveflag:
print("圖片文件夾所在路徑不存在!")
automakedir = input("是否自動(dòng)創(chuàng)建對(duì)應(yīng)文件夾?(是Y/否N):")
if automakedir.strip().upper() == "Y":
os.makedirs(savedirname)
saveflag = True
files = os.listdir(dirname)
reductionFactor = int(input("請(qǐng)輸入長(zhǎng)寬壓縮比(例如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("請(qǐng)輸入輸出文件名(例如:第一章):")
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))
到此這篇關(guān)于Python自動(dòng)化辦公之圖片轉(zhuǎn)PDF的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python圖片轉(zhuǎn)PDF內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python自制一個(gè)PDF轉(zhuǎn)PNG圖片小工具
- 詳解python實(shí)現(xiàn)多張多格式圖片轉(zhuǎn)PDF并打包成exe
- Python實(shí)現(xiàn)文字pdf轉(zhuǎn)換圖片pdf效果
- Python把圖片轉(zhuǎn)化為pdf代碼實(shí)例
- 利用python將圖片版PDF轉(zhuǎn)文字版PDF
- Python 將pdf轉(zhuǎn)成圖片的方法
- windows下Python實(shí)現(xiàn)將pdf文件轉(zhuǎn)化為png格式圖片的方法
- 用python 制作圖片轉(zhuǎn)pdf工具
- Python的pdfplumber庫(kù)將pdf轉(zhuǎn)為圖片的實(shí)現(xiàn)
相關(guān)文章
python 列表元素左右循環(huán)移動(dòng) 的多種解決方案
這篇文章主要介紹了python 列表元素左右循環(huán)移動(dòng) 的多種解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03
下載安裝好python后想查看python安裝位置的幾種方法
這篇文章主要介紹了在Windows系統(tǒng)中查看Python路徑和版本的幾種方法,并提供了一個(gè)清除命令行窗口的技巧,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2025-03-03
python使用標(biāo)準(zhǔn)庫(kù)根據(jù)進(jìn)程名如何獲取進(jìn)程的pid詳解
Python有一套很有用的標(biāo)準(zhǔn)庫(kù)(standard library)。標(biāo)準(zhǔn)庫(kù)會(huì)隨著Python解釋器,一起安裝在你的電腦中的,所以下面這篇文章主要給大家介紹了關(guān)于python使用標(biāo)準(zhǔn)庫(kù)根據(jù)進(jìn)程名如何獲取進(jìn)程pid的相關(guān)資料,需要的朋友可以參考下。2017-10-10
Python利用txt文件對(duì)Mysql進(jìn)行增刪改查移
這篇文章主要介紹了如何在Python中利用TXT文件對(duì)Mysql中的記錄進(jìn)行增刪改查移,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)學(xué)習(xí)2021-12-12
python3操作注冊(cè)表的方法(Url protocol)
使用python操作注冊(cè)表的方法最近學(xué)習(xí)了一下,現(xiàn)在做一下筆記,由于對(duì)Python語言的使用還不是很熟練,所以寫不出高大上的結(jié)構(gòu)2020-02-02
Python中實(shí)現(xiàn)輸入一個(gè)整數(shù)的案例
這篇文章主要介紹了Python中實(shí)現(xiàn)輸入一個(gè)整數(shù)的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05

