Python利用pptx操作PPT实现幻灯片的删除与替换
更新时间:2023年02月03日 08:51:12 作者:虚坏叔叔
这篇文章主要为大家详细介绍了python如何使用pptx库实现操作PPTx幻灯片文件删除并替换图片,文中的示例代码讲解详细,感兴趣的可以尝试一下
一、原理
通过查找ppt中的图片指纹替换
二、操作流程
原始ppt如下:


根据oldpic.png的md5指纹 找到图片
if md5img == md5finger:
slide.shapes.add_picture(newpic, shape.left, shape.top, shape.width, shape.height)
e.getparent().remove(e)
oldpic.png

想要替换的newpic.png

最后生成的成果如下:


三、代码
完整代码如下:
def replace_pic4shapes(filename, newpic, oldpic):
# 把旧样本图片Logo,获取指纹
imageFile = open(oldpic, "rb")
imgBlob = imageFile.read()
md5finger = hashlib.md5(imgBlob).hexdigest()
prs = Presentation(filename)
for slide in list(prs.slides)[0:]:
for shape in list(slide.shapes):
ispicture= False
try:
md5img = hashlib.md5(shape.image.blob).hexdigest()
ispicture = True
except:
pass
e = shape.element
if ispicture:
if md5img == md5finger:
slide.shapes.add_picture(newpic, shape.left, shape.top, shape.width, shape.height)
e.getparent().remove(e)
pass
prs.save("课件工坊-长征组歌新文件.pptx")
到此这篇关于Python利用pptx操作PPT实现幻灯片的删除与替换的文章就介绍到这了,更多相关Python PPT幻灯片删除替换内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
python批量检查两个对应的txt文件的行数是否一致的实例代码
这篇文章主要介绍了python批量检查两个对应的txt文件的行数是否一致,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-10-10
python base64 decode incorrect padding错误解决方法
这篇文章主要介绍了python base64 decode incorrect padding错误解决方法,本文使用把string补齐等号的方法解决了这个错误,需要的朋友可以参考下2015-01-01


最新评论