基于Python開(kāi)發(fā)PPT圖片提取與合并工具
在日常工作中,我們經(jīng)常需要處理PPT中的圖片,有時(shí)需要批量提取,有時(shí)需要將多張圖片合并成特定布局。本文將介紹如何使用Python開(kāi)發(fā)一個(gè)圖形界面工具,實(shí)現(xiàn)PPT圖片提取和九宮格合并功能。
全部代碼
import wx import os from pptx import Presentation from PIL import Image import io class MainFrame(wx.Frame): def __init__(self): super().__init__(parent=None, title='PPT圖片提取與合并工具', size=(600, 400)) self.ppt_path = '' self.output_dir = '' self.InitUI() def InitUI(self): panel = wx.Panel(self) vbox = wx.BoxSizer(wx.VERTICAL) # PPT選擇 hbox1 = wx.BoxSizer(wx.HORIZONTAL) self.ppt_text = wx.TextCtrl(panel) choose_btn = wx.Button(panel, label='選擇PPT') choose_btn.Bind(wx.EVT_BUTTON, self.OnChoosePPT) hbox1.Add(self.ppt_text, proportion=1, flag=wx.EXPAND|wx.ALL, border=5) hbox1.Add(choose_btn, flag=wx.ALL, border=5) # 輸出目錄選擇 hbox2 = wx.BoxSizer(wx.HORIZONTAL) self.dir_text = wx.TextCtrl(panel) dir_btn = wx.Button(panel, label='選擇輸出目錄') dir_btn.Bind(wx.EVT_BUTTON, self.OnChooseDir) hbox2.Add(self.dir_text, proportion=1, flag=wx.EXPAND|wx.ALL, border=5) hbox2.Add(dir_btn, flag=wx.ALL, border=5) # 提取按鈕 extract_btn = wx.Button(panel, label='提取圖片') extract_btn.Bind(wx.EVT_BUTTON, self.OnExtract) # 合并設(shè)置 hbox3 = wx.BoxSizer(wx.HORIZONTAL) row_label = wx.StaticText(panel, label='行數(shù):') self.row_text = wx.TextCtrl(panel, value='3') col_label = wx.StaticText(panel, label='列數(shù):') self.col_text = wx.TextCtrl(panel, value='3') merge_btn = wx.Button(panel, label='合并圖片') merge_btn.Bind(wx.EVT_BUTTON, self.OnMerge) hbox3.Add(row_label, flag=wx.ALL, border=5) hbox3.Add(self.row_text, flag=wx.ALL, border=5) hbox3.Add(col_label, flag=wx.ALL, border=5) hbox3.Add(self.col_text, flag=wx.ALL, border=5) hbox3.Add(merge_btn, flag=wx.ALL, border=5) # 狀態(tài)顯示 self.status_text = wx.TextCtrl(panel, style=wx.TE_MULTILINE|wx.TE_READONLY) # 添加到主布局 vbox.Add(hbox1, flag=wx.EXPAND) vbox.Add(hbox2, flag=wx.EXPAND) vbox.Add(extract_btn, flag=wx.EXPAND|wx.ALL, border=5) vbox.Add(hbox3, flag=wx.EXPAND) vbox.Add(self.status_text, proportion=1, flag=wx.EXPAND|wx.ALL, border=5) panel.SetSizer(vbox) def OnChoosePPT(self, event): dlg = wx.FileDialog(self, "選擇PPT文件", "", "", "PPT files (*.pptx)|*.pptx", wx.FD_OPEN) if dlg.ShowModal() == wx.ID_OK: self.ppt_path = dlg.GetPath() self.ppt_text.SetValue(self.ppt_path) dlg.Destroy() def OnChooseDir(self, event): dlg = wx.DirDialog(self, "選擇輸出目錄") if dlg.ShowModal() == wx.ID_OK: self.output_dir = dlg.GetPath() self.dir_text.SetValue(self.output_dir) dlg.Destroy() def OnExtract(self, event): if not self.ppt_path or not self.output_dir: wx.MessageBox('請(qǐng)選擇PPT文件和輸出目錄', '錯(cuò)誤') return try: prs = Presentation(self.ppt_path) image_count = 0 for slide in prs.slides: for shape in slide.shapes: if hasattr(shape, "image"): image_stream = io.BytesIO(shape.image.blob) image = Image.open(image_stream) image_path = os.path.join(self.output_dir, f'image_{image_count}.png') image.save(image_path) image_count += 1 self.status_text.AppendText(f'成功提取{image_count}張圖片\n') except Exception as e: wx.MessageBox(f'提取圖片時(shí)出錯(cuò): {str(e)}', '錯(cuò)誤') def OnMerge(self, event): try: rows = int(self.row_text.GetValue()) cols = int(self.col_text.GetValue()) except ValueError: wx.MessageBox('請(qǐng)輸入有效的行數(shù)和列數(shù)', '錯(cuò)誤') return if not self.output_dir: wx.MessageBox('請(qǐng)選擇輸出目錄', '錯(cuò)誤') return try: # 獲取所有圖片文件 image_files = [f for f in os.listdir(self.output_dir) if f.startswith('image_') and f.endswith('.png')] image_files.sort() if len(image_files) < rows * cols: wx.MessageBox('圖片數(shù)量不足', '錯(cuò)誤') return # 讀取第一張圖片獲取尺寸 first_image = Image.open(os.path.join(self.output_dir, image_files[0])) img_width, img_height = first_image.size # 創(chuàng)建合并后的畫(huà)布 merged = Image.new('RGB', (img_width * cols, img_height * rows)) # 拼接圖片 for idx, img_file in enumerate(image_files[:rows * cols]): if idx >= rows * cols: break img = Image.open(os.path.join(self.output_dir, img_file)) x = (idx % cols) * img_width y = (idx // cols) * img_height merged.paste(img, (x, y)) # 保存合并后的圖片 merged_path = os.path.join(self.output_dir, 'merged.png') merged.save(merged_path) self.status_text.AppendText(f'成功合并圖片: {merged_path}\n') except Exception as e: wx.MessageBox(f'合并圖片時(shí)出錯(cuò): {str(e)}', '錯(cuò)誤') def main(): app = wx.App() frame = MainFrame() frame.Show() app.MainLoop() if __name__ == '__main__': main()
功能概述
這個(gè)工具主要實(shí)現(xiàn)兩個(gè)核心功能:
從PPT文檔中批量提取所有圖片
將提取的圖片按照指定的行列數(shù)合并成九宮格布局
技術(shù)棧選擇
為了實(shí)現(xiàn)這個(gè)工具,我們選用了以下Python庫(kù):
wxPython:用于創(chuàng)建圖形用戶(hù)界面
python-pptx:用于處理PPT文檔
Pillow(PIL):用于圖片處理和合并
io:用于處理二進(jìn)制數(shù)據(jù)流
詳細(xì)設(shè)計(jì)
1. 用戶(hù)界面設(shè)計(jì)
我們使用wxPython創(chuàng)建了一個(gè)簡(jiǎn)潔的圖形界面,包含以下組件:
- PPT文件選擇區(qū)域
- 輸出目錄選擇區(qū)域
- 圖片提取按鈕
- 行列數(shù)輸入框
- 圖片合并按鈕
- 狀態(tài)顯示文本框
界面布局采用垂直和水平布局器(BoxSizer)組合,確保各個(gè)組件能夠合理排列和自適應(yīng)窗口大小。
2. 核心功能實(shí)現(xiàn)
PPT圖片提取功能
def OnExtract(self, event): if not self.ppt_path or not self.output_dir: wx.MessageBox('請(qǐng)選擇PPT文件和輸出目錄', '錯(cuò)誤') return try: prs = Presentation(self.ppt_path) image_count = 0 for slide in prs.slides: for shape in slide.shapes: if hasattr(shape, "image"): image_stream = io.BytesIO(shape.image.blob) image = Image.open(image_stream) image_path = os.path.join(self.output_dir, f'image_{image_count}.png') image.save(image_path) image_count += 1 self.status_text.AppendText(f'成功提取{image_count}張圖片\n') except Exception as e: wx.MessageBox(f'提取圖片時(shí)出錯(cuò): {str(e)}', '錯(cuò)誤')
這段代碼通過(guò)python-pptx庫(kù)打開(kāi)PPT文檔,遍歷所有幻燈片和形狀,找到圖片類(lèi)型的形狀后,將其轉(zhuǎn)換為PIL Image對(duì)象并保存到指定目錄。
圖片合并功能
def OnMerge(self, event): try: rows = int(self.row_text.GetValue()) cols = int(self.col_text.GetValue()) except ValueError: wx.MessageBox('請(qǐng)輸入有效的行數(shù)和列數(shù)', '錯(cuò)誤') return # ... 獲取圖片文件列表 ... # 創(chuàng)建合并后的畫(huà)布 merged = Image.new('RGB', (img_width * cols, img_height * rows)) # 拼接圖片 for idx, img_file in enumerate(image_files[:rows * cols]): if idx >= rows * cols: break img = Image.open(os.path.join(self.output_dir, img_file)) x = (idx % cols) * img_width y = (idx // cols) * img_height merged.paste(img, (x, y)) merged.save(os.path.join(self.output_dir, 'merged.png'))
合并功能首先創(chuàng)建一個(gè)足夠大的空白畫(huà)布,然后按照行列順序?qū)D片粘貼到對(duì)應(yīng)位置。
使用說(shuō)明
環(huán)境準(zhǔn)備
在使用此工具前,需要安裝必要的Python庫(kù):
pip install wxPython python-pptx Pillow
使用步驟
運(yùn)行程序
點(diǎn)擊"選擇PPT"按鈕,選擇要處理的PPT文件
點(diǎn)擊"選擇輸出目錄"按鈕,選擇圖片保存位置
點(diǎn)擊"提取圖片"按鈕,程序會(huì)自動(dòng)提取PPT中的所有圖片
輸入想要的行數(shù)和列數(shù)(默認(rèn)3x3)
點(diǎn)擊"合并圖片"按鈕,程序會(huì)生成合并后的圖片
結(jié)果如下
注意事項(xiàng)
- PPT文件必須是.pptx格式
- 確保有足夠的磁盤(pán)空間存儲(chǔ)提取的圖片
- 合并時(shí),建議使用相同尺寸的圖片,否則可能會(huì)出現(xiàn)布局不均勻的情況
- 圖片數(shù)量應(yīng)不少于行數(shù)×列數(shù),否則會(huì)提示錯(cuò)誤
以上就是基于Python開(kāi)發(fā)PPT圖片提取與合并工具的詳細(xì)內(nèi)容,更多關(guān)于Python PPT圖片提取與合并的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python 實(shí)現(xiàn)的車(chē)牌識(shí)別項(xiàng)目
這篇文章主要介紹了python 實(shí)現(xiàn)的車(chē)牌識(shí)別項(xiàng)目,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-01-01python通過(guò)imaplib模塊讀取gmail里郵件的方法
這篇文章主要介紹了python通過(guò)imaplib模塊讀取gmail里郵件的方法,涉及Python操作imaplib模塊操作郵件的相關(guān)技巧,需要的朋友可以參考下2015-05-05Python?數(shù)據(jù)類(lèi)型中的字符串和數(shù)字
這篇文章主要介紹了Python?數(shù)據(jù)類(lèi)型中的字符串和數(shù)字,Python3中有六個(gè)標(biāo)準(zhǔn)的數(shù)據(jù)類(lèi)型,Number、String、List、Tuple、Set、Dictionary,加先來(lái)我們就來(lái)看看這幾種數(shù)據(jù)類(lèi)型的具體相關(guān)介紹,需要的小伙伴可以參考一下2022-02-02Python實(shí)現(xiàn)模擬錕斤拷等各類(lèi)亂碼詳解
說(shuō)到亂碼問(wèn)題就不得不提到錕斤拷,這算是非常常見(jiàn)的一種亂碼形式,那么它到底是經(jīng)過(guò)何種錯(cuò)誤操作產(chǎn)生的呢?本文我們就來(lái)一步步探究2023-02-02Python 函數(shù)繪圖及函數(shù)圖像微分與積分
今天小編就為大家分享一篇Python 函數(shù)繪圖及函數(shù)圖像微分與積分,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11Python如何使用print()函數(shù)輸出格式化字符串
Python中內(nèi)置的%操作符和format函數(shù),都可以用于格式化字符串,下面這篇文章主要給大家介紹了關(guān)于Python如何使用print()函數(shù)輸出格式化字符串的相關(guān)資料,需要的朋友可以參考下2021-08-08Python遍歷指定文件夾下的所有文件名的方法小結(jié)
當(dāng)需要遍歷指定文件夾下的所有文件名時(shí),Python提供了多種方法來(lái)實(shí)現(xiàn)這個(gè)任務(wù),本文將介紹如何使用Python來(lái)完成這一任務(wù),有需要的小伙伴可以參考下2024-01-01