python實(shí)現(xiàn)pptx批量向PPT中插入圖片
項(xiàng)目背景
實(shí)驗(yàn)結(jié)果拍攝了一組圖片,數(shù)量較大,想要按順序組合排版,比較簡(jiǎn)單的方式是在PPT中插入圖片進(jìn)行排版。但是PPT批量插入圖片后,順序打亂且不顯示圖片名稱,每個(gè)圖片單獨(dú)調(diào)整位置和大小費(fèi)時(shí)費(fèi)力,于是想到使用工具批量操作。過(guò)去了解過(guò)python自動(dòng)化辦公模塊,相對(duì)來(lái)說(shuō)python也是簡(jiǎn)單易用的語(yǔ)言,項(xiàng)目預(yù)計(jì)不會(huì)耗費(fèi)太大精力,故嘗試學(xué)習(xí)實(shí)踐一番。(非專業(yè)學(xué)習(xí)筆記分享,望各位大佬不吝指導(dǎo)!)
數(shù)據(jù)為16組實(shí)驗(yàn),每組實(shí)驗(yàn)重復(fù)兩次,共32個(gè)圖片,所有圖片為正方形,
命名方式為:
1.png,1-5.png,2.png,2-5.png … … … … 16.png,16-5.png
需嚴(yán)格按照順序排列
基礎(chǔ)
安裝
pip install python-pptx
依賴
Python 2.6, 2.7, 3.3, 3.4, or 3.6
lxml
Pillow
XlsxWriter (to use charting features)
基礎(chǔ)操作代碼概覽:
import collections.abc from pptx import Presentation, util prs = Presentation() # 實(shí)例化一個(gè)ppt演示文稿對(duì)象 blank_slide_layout = prs.slide_layouts[6] # 實(shí)例化空白模板 slide = prs.slides.add_slide(blank_slide_layout) # 向文件中添加空白頁(yè)面 img_path = './1.png' # 圖片路徑 # 設(shè)置圖片的位置和大小 left = util.Cm(0) top = util.Cm(0) width = util.Cm(4) height = util.Cm(4) # 在頁(yè)面中插入圖片 pic = slide.shapes.add_picture(img_path, left, top, width, height) prs.save('自動(dòng)生成的ppt.pptx') # 保存為文件
第一步:建立一個(gè)PPT文件
from pptx import Presentation prs = Presentation() # 實(shí)例化一個(gè)ppt演示文稿對(duì)象 # 中間補(bǔ)充具體操作添加內(nèi)容 prs.save('自動(dòng)生成的ppt.pptx') # 保存為文件
此時(shí)踩了第一個(gè)坑,執(zhí)行結(jié)果報(bào)錯(cuò):AttributeError: module 'collections' has no attribute 'Container'
原因是python 3.10版本支持問(wèn)題,此時(shí)在開(kāi)頭多導(dǎo)入一個(gè)依賴包c(diǎn)ollections.abc即可解決。
import collections.abc
第二步:新建頁(yè)面
prs.slide_layouts是Presentation對(duì)象的默認(rèn)頁(yè)面模板,是一個(gè)數(shù)組,共11個(gè),可通過(guò)循環(huán)查看所有默認(rèn)頁(yè)面模板。
prs.slides.add_slide( )方法可向文件中添加模板頁(yè)面。默認(rèn)第7個(gè)模板為空白頁(yè)面。
n = len(prs.slide_layouts) print("頁(yè)面模板數(shù)量:", n) for i in range(n): slide_layout = prs.slide_layouts[i] # 實(shí)例化模板頁(yè)面 slide = prs.slides.add_slide(slide_layout) # 向文件中添加模板頁(yè)面
單獨(dú)添加一個(gè)空白頁(yè)面僅需如下代碼:
blank_slide_layout = prs.slide_layouts[6] # 實(shí)例化空白模板頁(yè)面 slide = prs.slides.add_slide(blank_slide_layout) # 向文件中添加空白頁(yè)面
第三步:添加圖片
添加圖片可使用如下方法,
pic = slide.shapes.add_picture(img_path, left, top, width, height)
位置和大小屬性默認(rèn)為英制單位EMU,可轉(zhuǎn)化為厘米,用如下方法定義:
from pptx import util img_path = './1.png' # 圖片路徑 left = util.Cm(0) top = util.Cm(0) width = util.Cm(4) height = util.Cm(4)
此時(shí)便可得到一個(gè)在左上角插入圖片的頁(yè)面。
加億點(diǎn)點(diǎn)細(xì)節(jié)
1. 改變幻燈片頁(yè)面大小
默認(rèn)生成的頁(yè)面大小為 4 : 3 大小的頁(yè)面畫布,可通過(guò)修改Presentation對(duì)象的屬性改變大小,如下:
prs.slide_width = util.Cm(32) prs.slide_height = util.Cm(18)
2. 根據(jù)需要排列圖片位置
# 讀取圖片列表 pic_list = [] for i in listdir(): if '.png' in i: pic_list.append(i) print('圖片列表:\n', pic_list) # 設(shè)置圖片的大小 width = util.Cm(4) height = util.Cm(4) for p in pic_list: # 圖片路徑 img_path = './' + p # 設(shè)置圖片位置 n = pic_list.index(p) if n < 16: if '-' not in p: top = util.Cm(0) left = util.Cm((n - 1) * 2) else: top = util.Cm(5) left = util.Cm(n * 2) else: if '-' not in p: top = util.Cm(10) left = util.Cm((n - 17) * 2) else: top = util.Cm(15) left = util.Cm((n - 16) * 2) # 在頁(yè)面中插入圖片 pic = slide.shapes.add_picture(img_path, left, top, width, height)
最終代碼
import collections.abc from pptx import Presentation, util from os import listdir # 實(shí)例化一個(gè)ppt演示文稿對(duì)象 prs = Presentation() # 調(diào)整頁(yè)面大小 prs.slide_width = util.Cm(32) prs.slide_height = util.Cm(19) # 實(shí)例化空白模板 blank_slide_layout = prs.slide_layouts[6] # 向文件中添加空白頁(yè)面 slide = prs.slides.add_slide(blank_slide_layout) # 讀取圖片列表 pic_list = [] for i in listdir(): if '.png' in i: pic_list.append(i) print('圖片列表:\n', pic_list) # 設(shè)置圖片的大小 width = util.Cm(4) height = util.Cm(4) for p in pic_list: # 圖片路徑 img_path = './' + p # 設(shè)置圖片位置 n = pic_list.index(p) if n < 16: if '-' not in p: top = util.Cm(0) left = util.Cm((n - 1) * 2) else: top = util.Cm(5) left = util.Cm(n * 2) else: if '-' not in p: top = util.Cm(10) left = util.Cm((n - 17) * 2) else: top = util.Cm(15) left = util.Cm((n - 16) * 2) # 在頁(yè)面中插入圖片 pic = slide.shapes.add_picture(img_path, left, top, width, height) # 保存為文件 prs.save('自動(dòng)生成的ppt.pptx')
項(xiàng)目結(jié)果圖
總結(jié)
到此這篇關(guān)于python實(shí)現(xiàn)pptx批量向PPT中插入圖片的文章就介紹到這了,更多相關(guān)python pptx向PPT插圖片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python連接clickhouse數(shù)據(jù)庫(kù)的兩種方式小結(jié)
- Python?+?Tkinter連接本地MySQL數(shù)據(jù)庫(kù)簡(jiǎn)單實(shí)現(xiàn)注冊(cè)登錄
- 如何利用Python連接MySQL數(shù)據(jù)庫(kù)實(shí)現(xiàn)數(shù)據(jù)儲(chǔ)存
- 分析解決Python中sqlalchemy數(shù)據(jù)庫(kù)連接池QueuePool異常
- python數(shù)據(jù)庫(kù)批量插入數(shù)據(jù)的實(shí)現(xiàn)(executemany的使用)
- Python操控mysql批量插入數(shù)據(jù)的實(shí)現(xiàn)方法
- python消費(fèi)kafka數(shù)據(jù)批量插入到es的方法
- python文件讀寫并使用mysql批量插入示例分享(python操作mysql)
- Python連接數(shù)據(jù)庫(kù)并批量插入包含日期記錄的操作
相關(guān)文章
使用opencv相關(guān)函數(shù)確定圖片中的直線問(wèn)題
這篇文章主要介紹了使用opencv相關(guān)函數(shù)確定圖片中的直線問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11python為什么會(huì)環(huán)境變量設(shè)置不成功
在本篇文章里小編給大家分享的是一篇關(guān)于python環(huán)境變量設(shè)置不成功怎么辦的解決方法內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)下。2020-06-06Python代碼顯得Pythonic(區(qū)別于其他語(yǔ)言的寫法)
這篇文章主要介紹了Python代碼顯得Pythonic(區(qū)別于其他語(yǔ)言的寫法),對(duì)于字符串連接,相比于簡(jiǎn)單的+,更pythonic的做法是盡量使用%操作符或者format函數(shù)格式化字符串,感興趣的小伙伴和小編一起進(jìn)入文章了解更詳細(xì)相關(guān)知識(shí)內(nèi)容吧2022-02-02Python基于checksum計(jì)算文件是否相同的方法
這篇文章主要介紹了Python基于checksum計(jì)算文件是否相同的方法,涉及Python針對(duì)二進(jìn)制文件的讀取與判定技巧,需要的朋友可以參考下2015-07-07VSCode配置python環(huán)境及中文問(wèn)題解決方法
這篇文章主要介紹了VSCode配置python環(huán)境及中文問(wèn)題,print打印中文亂碼如何解決這個(gè)問(wèn)題呢,本文給大家?guī)?lái)兩種方法幫助大家解決這個(gè)問(wèn)題,需要的朋友可以參考下2022-02-02PyTorch?Dataset與DataLoader使用超詳細(xì)講解
用于處理數(shù)據(jù)樣本的代碼可能會(huì)變得凌亂且難以維護(hù);理想情況下,我們希望數(shù)據(jù)集代碼與模型訓(xùn)練代碼解耦,以獲得更好的可讀性和模塊化。PyTorch提供的torch.utils.data.DataLoader和torch.utils.data.Dataset允許你使用預(yù)下載的數(shù)據(jù)集或自己制作的數(shù)據(jù)2022-10-10Python Pickling 和 Unpickling 的區(qū)別
Python中的Pickling和Unpickling是與數(shù)據(jù)序列化和反序列化相關(guān)的重要概念,本文主要介紹了Python Pickling和Unpickling的區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11