Python实现提取或替换PPT中文本与图片的示例代码
作者:虚坏叔叔
这篇文章主要为大家详细介绍了Python如何实现提取保存ppt中的图片和替换ppt模板的文本,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
提取保存ppt中的图片
如何从pptx中提取所有图片?用python-pptx轻松实现图片提取
从指定的文件夹中,对所有pptx(注意不是ppt,因为两者文档格式不同)进行图片提取。
提取出来的图片,以图片原有名称作为文件名,如果遇到文件名有相同,则在文件名后随机加上数字,保存位置为程序中设定的targetPath,如果该目录不存在的话,则会先创建一个。
示例代码
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__': # 要进行提取的pptx的所在目录 "targetPath":r"K:\伍德春原创视频\自动化\2020-11-10\img", # 提取后的txt文件要保存到的目录 params = { "sourcePath":r"D:\自动化", "targetPath":r"D:\自动化\img",} newobj = ExtractPPTXimg(params)
替换ppt模板的文本
我要做的就是:利用python自动控制ppt,动态修改我们指定的变量参数,
把组合出来的幻灯片保存为一张张的图片,然后在这个基础上加入批量化!
示例代码
from pptx import Presentation from pptx.enum.shapes import MSO_SHAPE_TYPE def ppt_catch_format_text(filename): """ 抓取PPT的内容,按段落返回 其中 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")
到此这篇关于Python实现提取或替换PPT中文本与图片的示例代码的文章就介绍到这了,更多相关Python PPT文本图片内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!