Python實現(xiàn)提取或替換PPT中文本與圖片的示例代碼
提取保存ppt中的圖片
如何從pptx中提取所有圖片?用python-pptx輕松實現(xiàn)圖片提取
從指定的文件夾中,對所有pptx(注意不是ppt,因為兩者文檔格式不同)進(jìn)行圖片提取。
提取出來的圖片,以圖片原有名稱作為文件名,如果遇到文件名有相同,則在文件名后隨機(jī)加上數(shù)字,保存位置為程序中設(shè)定的targetPath,如果該目錄不存在的話,則會先創(chuàng)建一個。
示例代碼
import os
import re,random
from pptx import Presentation
# coding=gbkimport osimport refrom pptx import Presentationimport random
class ExtractPPTXimg():
def __init__(self,params):
self.errFlag = False
self.msg = ""
self.sourcePath = params["sourcePath"]
if not os.path.exists(self.sourcePath):
self.errFlag = True
self.msg = "源文件夾不存在!"
self.targetPath = params["targetPath"]
if not os.path.exists(self.targetPath):
os.makedirs(self.targetPath)
self.run()
def run(self):
if self.errFlag:
print(self.msg)
return
for file in os.listdir(self.sourcePath):
if not file[-4:] == "pptx":
continue
if re.findall("^~",file):
continue
# 提取圖片
self.extractImg(file)
# 保存pptx中的圖片
def extractImg(self,file):
fileName,expadName = os.path.splitext(file)
prs = Presentation(os.path.join(self.sourcePath,file))
for slide in prs.slides:
for shape in slide.shapes:
try:
if "image" in shape.image.content_type:
imgName = shape.image.filename
newPath = os.path.join(self.targetPath, fileName)
if not os.path.exists(newPath):
os.makedirs(newPath)
newFile = os.path.join(newPath, imgName)
self.saveImage(newFile,shape.image.blob)
except:
continue
# 保存圖片
def saveImage(self,newFile,blob):
if os.path.exists(newFile):
fileName, expadName = os.path.splitext(newFile)
newFile = "{}-{}{}".format(fileName,random.randint(1,1000),expadName)
with open(newFile, "wb") as f:
f.write(blob)
print("已保存{}".format(newFile))
def __str__(self):
return self.msg
if __name__ == '__main__':
# 要進(jìn)行提取的pptx的所在目錄 "targetPath":r"K:\伍德春原創(chuàng)視頻\自動化\2020-11-10\img", # 提取后的txt文件要保存到的目錄
params = { "sourcePath":r"D:\自動化", "targetPath":r"D:\自動化\img",}
newobj = ExtractPPTXimg(params)
替換ppt模板的文本
我要做的就是:利用python自動控制ppt,動態(tài)修改我們指定的變量參數(shù),
把組合出來的幻燈片保存為一張張的圖片,然后在這個基礎(chǔ)上加入批量化!
示例代碼
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE_TYPE
def ppt_catch_format_text(filename):
"""
抓取PPT的內(nèi)容,按段落返回
其中 filename 是PPT文件的路徑
"""
search_str = '性能探測器'
repl_str = '性能探測器PPT啦qqqqqq'
prs = Presentation(filename)
for x in range(len(prs.slides)):
# ---Only on text-boxes outside group elements---
for shape in prs.slides[x].shapes:
if hasattr(shape, "text"):
if(shape.text.find(search_str))!=-1:
text_frame = shape.text_frame
cur_texts = text_frame.paragraphs[0].runs
for index in range(len(cur_texts)):
print(text_frame.paragraphs[0].runs[index].text)
if(cur_texts[index].text.find(search_str))!=-1:
#print(5566)
#print(cur_texts[index].text)
cur_text = text_frame.paragraphs[0].runs[index].text
new_text = cur_text.replace(str(search_str), str(repl_str))
text_frame.paragraphs[0].runs[index].text = new_text
# ---Only operate on group shapes---
group_shapes = [shp for shp in prs.slides[x].shapes
if shp.shape_type ==MSO_SHAPE_TYPE.GROUP]
#print(group_shapes)
for group_shape in group_shapes:
for shape in group_shape.shapes:
if shape.has_text_frame:
if(shape.text.find(search_str))!=-1:
text_frame = shape.text_frame
# cur_texts = text_frame.paragraphs[0].runs
for index in range(len(text_frame.paragraphs)):
cur_text = text_frame.paragraphs[index].text
#print(cur_texts[index].text.encode('utf-8').strip().decode())
if(cur_text.find(search_str))!=-1:
print(7788)
#print(cur_texts[index].text)
new_text = cur_text.replace(str(search_str), str(repl_str))
text_frame.paragraphs[index].text = new_text
#print(cur_text)
prs.save('D:\自動化\ss.pptx')
ppt_catch_format_text(r"D:\自動化\課件.pptx")
到此這篇關(guān)于Python實現(xiàn)提取或替換PPT中文本與圖片的示例代碼的文章就介紹到這了,更多相關(guān)Python PPT文本圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python調(diào)用動態(tài)鏈接庫的基本過程詳解
這篇文章主要介紹了python調(diào)用動態(tài)鏈接庫的基本過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
python Qt5實現(xiàn)窗體跟蹤鼠標(biāo)移動
今天小編就為大家分享一篇python Qt5實現(xiàn)窗體跟蹤鼠標(biāo)移動,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python中的命令行參數(shù)解析工具之docopt詳解
docopt 是一個用來解析命令行參數(shù)的工具,當(dāng)想要在 Python 程序后面附加參數(shù)時,就不需要再為此而發(fā)愁了。下面這篇文章主要介紹了Python中命令行參數(shù)解析工具之docopt的相關(guān)資料,介紹的非常詳細(xì),需要的朋友們下面來一起看看吧。2017-03-03
Python使用虛擬環(huán)境(安裝下載更新卸載)命令
這篇文章主要為大家介紹了Python使用虛擬環(huán)境(安裝下載更新卸載)命令,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
Gradio機(jī)器學(xué)習(xí)模型快速部署工具quickstart前篇
這篇文章主要為大家介紹了Gradio機(jī)器學(xué)習(xí)模型快速部署工具quickstart準(zhǔn)備原文翻譯,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
淺談pandas篩選出表中滿足另一個表所有條件的數(shù)據(jù)方法
今天小編就為大家分享一篇淺談pandas篩選出表中滿足另一個表所有條件的數(shù)據(jù)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02

