当前位置: 代码网 > it编程>前端脚本>Python > Python实现在PPT中添加或删除图片

Python实现在PPT中添加或删除图片

2025年04月28日 Python 我要评论
在数字化办公时代,python已成为自动化处理office文档的首选工具。针对powerpoint图片管理,使用python能实现批量操作多个ppt文件,并减少人工操作的失误。本文将介绍如何使用免费p

在数字化办公时代,python已成为自动化处理office文档的首选工具。针对powerpoint图片管理,使用python能实现批量操作多个ppt文件,并减少人工操作的失误。本文将介绍如何使用免费python库在ppt文档中添加图片,或删除ppt文档中的图片

环境准备

安装 free spire.presentation for python库。

pip install spire.presentation.free

python 在ppt幻灯片中添加图片

使用免费python库提供的 islide.shapes.appendembedimagebyimagedata() 方法,可实现在指定幻灯片中添加图片。操作如下:

创建ppt演示文档,并通过 presentation.slides[index] 属性获取其中指定幻灯片。

加载一张图片,然后指定图片在幻灯片上的位置。

通过 islide.shapes.appendembedimagebypath() 方法在幻灯片中添加图片。

使用 presentation.savetofile() 方法保存生成的演示文稿。

python代码:

from spire.presentation.common import *
import math
from spire.presentation import *

# 创建ppt演示文稿
presentation = presentation()

# 获取第一张幻灯片
slide = presentation.slides[0]

# 加载一张图片
imagefile = "logo.png"

# 指定图片在幻灯片中的位置
left = math.trunc(presentation.slidesize.size.width / float(2)) -100
rect1 = rectanglef.fromltrb (left, 120, 180 + left, 300)

# 在幻灯片上添加嵌入式图片
image = slide.shapes.appendembedimagebypath (shapetype.rectangle, imagefile, rect1)

# 设置图片的外框线为无填充效果
image.line.filltype = fillformattype.none

# 保存ppt文档
presentation.savetofile("ppt图片.pptx", fileformat.pptx2016)
presentation.dispose()

生成文档:

python 删除ppt幻灯片中的图片

要删除powerpoint幻灯片中的所有图片则需要先遍历幻灯片中的每一个形状,找出形状中的图片类型 (slidepicture),再通过 islide.shapes.remove(index) 来删除图片。操作如下:

使用 presentation.loadfromfile() 方法加载ppt文档。

通过 presentation.slides[index] 属性获取指定幻灯片。

使用 for 循环来遍历指定幻灯片上的所有形状。

判断当前形状是否是为 slidepicture 类型(图片),如果是,则使用 islide.shapes.remove(index) 来删除图片。

使用 presentation.savetofile() 方法保存生成的演示文稿。

python代码:

from spire.presentation.common import *
from spire.presentation import *

# 加载ppt文件
ppt = presentation()
ppt.loadfromfile("ppt图片.pptx")

# 获取第一张幻灯片
slide = ppt.slides[0]

# 遍历幻灯片中的所有形状
for i in range(slide.shapes.count - 1, -1, -1):
   
    # 判断当前形状是否为图片(slidepicture)
    if isinstance(slide.shapes[i], slidepicture):
       
        # 如果是图片,则从幻灯片中移除该形状
        slide.shapes.removeat(i)

# 保存结果文件
ppt.savetofile("删除ppt图片.pptx", fileformat.pptx2016)
ppt.dispose()

方法补充

python向ppt中批量插入图片

1.用python-pptx模块

实现的过程也很简单,主要是导入指定模块,利用os来遍历所有的图片,然后创建ppt对象,插入空白的slide,然后通过循环的办法把图片插入到幻灯片中。根据图片距离左、上、高度来最终确定其位置。

from pptx import presentation
from pptx.util import inches
import os
 
# 获取当前目录下所有的png文件
pics = [file for file in os.listdir(".") if file.endswith(".png")]
 
# 创建一个演示文稿对象
prs = presentation()
 
for pic in pics:
    # 添加一张新幻灯片
    slide_layout = prs.slide_layouts[5]  # 使用空白布局
    slide = prs.slides.add_slide(slide_layout)
 
    # 添加图片到幻灯片
    img_path = pic
    left = inches(5.8)
    top = inches(2)
    height = inches(3.5)
    slide.shapes.add_picture(img_path, left, top, height=height)
 
# 保存演示文稿
prs.save('add-image-in-presentation.pptx')

2.利用aspose.slides这个模块

aspose模块的办法和python-pptx的类似。过程也是首先遍历得到图片的地址,然后创建ppt对象,通过添加空白slide,把图片插入的办法来实现。

import aspose.slides as slides
import os
pics = [file for file in os.listdir(".") if file.endswith(".png")]
 
    # 创建演示文稿
with slides.presentation() as pres:
    # 访问第一张幻灯片
    for num,pic in enumerate(pics,0):
        
        slide = pres.slides.add_empty_slide(pres.layout_slides[0])
 
        # 从文件加载图像
        with open(pic, "rb") as in_file:
          
            # 将图像添加到演示文稿的图像集
            image = pres.images.add_image(in_file)
 
            # 将图像添加到幻灯片
            slide.shapes.add_picture_frame(slides.shapetype.rectangle, 20, 20, 100, 100, image)
 
    # 保存演示文稿
    pres.save("add-image-in-presentation.pptx", slides.export.saveformat.pptx)

slide.shapes.add_picture_frame(slides.shapetype.rectangle, 20, 20, 100, 100, image)这行代码中,前2个数是是图片距离左侧和右侧的距离。

到此这篇关于python实现在ppt中添加或删除图片的文章就介绍到这了,更多相关python ppt图片内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com