一、原理

通过查找ppt中的图片指纹替换

二、操作流程

原始ppt如下:

Python利用pptx操作PPT实现幻灯片的删除与替换

Python利用pptx操作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

Python利用pptx操作PPT实现幻灯片的删除与替换

想要替换的newpic.png

Python利用pptx操作PPT实现幻灯片的删除与替换

最后生成的成果如下:

Python利用pptx操作PPT实现幻灯片的删除与替换

Python利用pptx操作PPT实现幻灯片的删除与替换

三、代码

完整代码如下:

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")
原文地址:https://blog.csdn.net/biggbang/article/details/128848925

相关文章:

  • 2022-12-23
  • 2021-12-27
  • 2021-12-21
  • 2023-01-12
  • 2021-11-23
  • 2021-12-02
  • 2021-10-06
猜你喜欢
  • 2021-07-20
  • 2021-06-19
  • 2022-01-17
  • 2021-06-08
  • 2021-08-17
  • 2022-12-23
  • 2021-12-31
相关资源
相似解决方案