186-6908-0178
当前位置:首页 >程序开发
使用 Python 操作 PowerPoint 中的图片
2026-09-30 43次浏览

演示文稿里图片几乎不可或缺:公司 Logo、产品截图、流程图、配图都靠图片传达信息。往幻灯片上放一张图片看似简单,但要精确控制位置、大小、边框和透明度,或者在多份文稿里批量替换旧 Logo,手动操作就不够用了。

本文以 Spire.Presentation for Python 为例,介绍如何在幻灯片上插入图片、缩放图片尺寸、设置图片框的边框和旋转角度、给图片加透明度、替换已有图片,以及批量删除幻灯片上的图片。

环境准备

安装 Spire.Presentation 库:

bash代码解读复制代码pip install Spire.Presentation

在脚本中引入模块:

python代码解读复制代码from spire.presentation import * from spire.presentation.common import *

在幻灯片上插入图片

AppendEmbedImageByPath() 把图片嵌入到指定幻灯片的矩形区域内,返回一个图片形状对象:

python代码解读复制代码import math  presentation = Presentation() presentation.LoadFromFile
("演示文稿.pptx")  imagePath = "logo.png" # 计算居中位置 left = math.trunc(presentation.SlideSize.Size.
Width / float(2)) - 280 rect = RectangleF.FromLTRB(left, 140, 120 + left, 260)  
# 在第一张幻灯片插入图片 image = presentation.Slides[0].Shapes.AppendEmbedImageByPath
(     ShapeType.Rectangle, imagePath, rect) # 去掉默认边框 image.Line.FillType = FillFormatType.none
 presentation.SaveToFile("插入图片.pptx", FileFormat.Pptx2010) presentation.Dispose()
 


RectangleF.FromLTRB(left, top, right, bottom) 定义图片在幻灯片上的位置和大小,单位为像素。ShapeType.Rectangle 指定容器形状类型。插入后默认带边框,把 Line.FillType 设为 FillFormatType.none 即可去掉。

缩放幻灯片上的图片

已经存在的图片可以通过遍历 Shapes 集合找到,再按比例调整 Width 和 Height:

python代码解读复制代码presentation = Presentation() presentation.LoadFromFile("演示文稿.pptx")
 scale = 0.5 for slide in presentation.Slides:     for shape in slide.Shapes:       
   if isinstance(shape, IEmbedImage):             shape.Width = shape.Width * scale   
            shape.Height = shape.Height * scale  presentation.SaveToFile("缩放图片.pptx",
             FileFormat.Pptx2013) presentation.Dispose()
            

isinstance(shape, IEmbedImage) 用来筛选出图片形状,跳过文本框、表格等其他形状。scale = 0.5 把每张图片缩小到原来的一半。按比例缩放可以保持宽高比不变形。

设置图片框的边框和旋转

插入图片时返回的对象本身就是形状,可以设置边框线型、宽度和旋转角度:

python代码解读复制代码import math  presentation = Presentation()  stream = Stream("logo.png")
imageData = presentation.Images.AppendStream(stream) stream.Close()  # 以图片原始尺寸的一半插入 rect =
RectangleF.FromLTRB(     100, 100,     math.trunc(imageData.Width / float(2)) + 100,   
  math.trunc(imageData.Height / float(2)) + 100) pptImage = presentation.Slides[0].Shapes.
  AppendEmbedImageByImageData(     ShapeType.Rectangle, imageData, rect)  
  # 设置边框:实线、浅蓝色、宽度 5 pptImage.Line.FillFormat.FillType =
  FillFormatType.Solid pptImage.Line.FillFormat.SolidFillColor.Color =
   Color.get_LightBlue() pptImage.Line.Width = 5 # 旋转 -45 度 pptImage.Rotation = -45  
   presentation.SaveToFile("图片框格式.pptx", FileFormat.Pptx2013) presentation.Dispose()
  

AppendEmbedImageByImageData() 用已加载的图片数据插入,适合同一张图片要多次使用的场景——先 Images.AppendStream() 加载一次,再重复引用。Line.FillFormat 控制边框填充,Rotation 控制旋转角度(正值为顺时针,负值为逆时针)。

给图片设置透明度

透明度需要通过形状填充来实现:先把图片加载到文稿的图片集合,再画一个矩形形状并用该图片填充,最后设置透明度:

python代码解读复制代码from spire.presentation import *  presentation = Presentation() stream =
Stream("logo.png") imageData = presentation.Images.AppendStream(stream) stream.Close()  
# 从图片数据读取尺寸 width, height = imageData.Width, imageData.Height rect = RectangleF.FromLTRB(200,
100, width + 200, height + 100) shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle,
rect) shape.Line.FillType = FillFormatType.none  # 用图片填充形状 shape.Fill.FillType =
 FillFormatType.Picture shape.Fill.PictureFill.Picture.EmbedImage = presentation.Images[0] shape.
 Fill.PictureFill.FillType = PictureFillType.Stretch  # 设置透明度为 50% shape.Fill.PictureFill.
 Picture.Transparency = 50  presentation.SaveToFile("透明图片.pptx", FileFormat.Pptx2013)
  presentation.Dispose()
 

Images.AppendStream() 把图片加入文稿资源池并返回图片数据,从中可以直接读取 Width 和 Height,无需借助 Pillow。FillType = FillFormatType.Picture 把形状填充切换为图片,EmbedImage 指向资源池中的图片对象,PictureFillType.Stretch 让图片拉伸填满形状。Transparency 取值 0 到 100,数值越大越透明。这种方式适用于需要把图片当背景或水印的场景。

替换已有的图片

更新 Logo 或换图时,先把新图片加载到文稿的图片集合,再把它赋给目标图片形状的 EmbedImage:

python代码解读复制代码ppt = Presentation() ppt.LoadFromFile("演示文稿.pptx") slide = ppt.Slides[0]
 # 加载新图片 stream = Stream("new_logo.png") newImage = ppt.Images.AppendStream(stream)
 stream.Close()  # 找到标题为 "image1" 的图片并替换 for shape in slide.Shapes:   
   if isinstance(shape, SlidePicture):         if shape.AlternativeTitle == "image1":   
            shape.PictureFill.Picture.EmbedImage = newImage  ppt.SaveToFile
            ("替换图片.pptx", FileFormat.Pptx2013) ppt.Dispose()
            

SlidePicture 是幻灯片图片形状的类型。通过 AlternativeTitle 定位到要替换的图片,再把新图片数据赋给 PictureFill.Picture.EmbedImage,即可完成替换。AppendStream() 把新图片加入文稿的图片资源池,替换时只是把形状指向新的资源。

删除幻灯片上的图片

清理不需要的图片,遍历 Shapes 并倒序移除:

python代码解读复制代码ppt = Presentation() ppt.LoadFromFile("演示文稿.pptx") slide = ppt.Slides[0]
 for i in range(slide.Shapes.Count - 1, -1, -1):     if isinstance(slide.Shapes[i], SlidePicture):
          slide.Shapes.RemoveAt(i)  ppt.SaveToFile("删除图片.pptx", FileFormat.Pptx2013)
          ppt.Dispose()
          

isinstance(slide.Shapes[i], SlidePicture) 判断形状是否为图片,RemoveAt(i) 按索引删除。倒序遍历避免删除后索引错位。

实用提示

  • RectangleF.FromLTRB 的四个参数是左、上、右、下坐标,不是宽和高,容易写错。

  • 插入图片后默认有边框,需要纯图片效果时记得把 Line.FillType 设为 none。

  • 同一张图片多次使用时,先用 Images.AppendStream() 加载一次再引用,比每次 AppendEmbedImageByPath() 更省体积。

  • 设置透明度需要通过形状填充实现,直接对 IEmbedImage 设置 Transparency 不生效。

  • 处理完调用 presentation.Dispose() 释放资源。

总结

本文介绍了用 Python 在 PowerPoint 中处理图片的常用操作:用 AppendEmbedImageByPath() 在幻灯片上插入图片、遍历 IEmbedImage 缩放尺寸、设置图片框的边框和旋转、通过形状填充实现透明度、用 EmbedImage 替换已有图片,以及倒序遍历 SlidePicture 批量删除。把这些操作组合进演示文稿生成脚本,图片的插入和维护可以完全自动化。


作者:用户835629078051
链接:https://juejin.cn/post/7690958935053942784
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


回到顶部