在 PowerPoint 演示文稿制作中,形状是可视化内容的核心。手动调整多个形状的布局和格式不仅繁琐,还容易出错。通过 Python 脚本配合 Spire.Presentation 库,可以批量完成形状的组合、取消组合、3D 效果、阴影、渐变/图案/图片填充等操作,极大提升模板生成和复杂图形设计的效率。
环境准备
使用 pip 安装 Spire.Presentation:
[code=python]pip install Spire.Presentation
[/code]
该库提供完整的 PowerPoint 操作 API,支持形状的创建、格式化、组合及特效处理。
形状组合:将多个独立形状合并为一个逻辑单元
组合后可以整体移动、调整大小或复制,便于统一管理。
[code=python]from spire.presentation import *
ppt = Presentation()
slide = ppt.Slides[0]
# 添加背景
rect = RectangleF.FromLTRB(0, 0, ppt.SlideSize.Size.Width, ppt.SlideSize.Size.Height)
slide.Shapes.AppendEmbedImageByPath(ShapeType.Rectangle, "bg.png", rect)
slide.Shapes[0].Line.FillFormat.SolidFillColor.Color = Color.get_FloralWhite()
# 创建矩形和丝带形状
rectangle = slide.Shapes.AppendShape(
ShapeType.Rectangle,
RectangleF.FromLTRB(250, 180, 450, 220))
rectangle.Fill.FillType = FillFormatType.Solid
rectangle.Fill.SolidColor.KnownColor = KnownColors.SkyBlue
rectangle.Line.Width = 0.1
ribbon = slide.Shapes.AppendShape(
ShapeType.Ribbon2,
RectangleF.FromLTRB(290, 155, 410, 235))
ribbon.Fill.FillType = FillFormatType.Solid
ribbon.Fill.SolidColor.KnownColor = KnownColors.LightPink
ribbon.Line.Width = 0.1
# 将两个形状加入列表并组合
arr = [rectangle, ribbon]
slide.GroupShapes(arr)
ppt.SaveToFile("GroupShapes_out.pptx", FileFormat.Pptx2013)
ppt.Dispose()
[/code]
注意:AppendShape 方法创建形状,GroupShapes 接收形状列表完成组合。组合后所有子形状将作为一个整体。
取消组合:编辑组合中的单个形状
当需要单独调整某个子形状时,可以取消组合:
[code=python]from spire.presentation import *
ppt = Presentation()
ppt.LoadFromFile("GroupShapes.pptx")
slide = ppt.Slides[0]
# 获取组合形状(假设它是幻灯片上的第一个形状且是 GroupShape 类型)
group = slide.Shapes[0] if isinstance(slide.Shapes[0], GroupShape) else None
if group:
slide.Ungroup(group)
ppt.SaveToFile("UngroupShapes.pptx", FileFormat.Pptx2013)
ppt.Dispose()
[/code]
取消组合后,子形状恢复为独立对象,可分别编辑。
为形状添加 3D 效果
通过设置材质、斜角和轮廓,让平面形状呈现立体感:
[code=python]from spire.presentation import *
ppt = Presentation()
slide = ppt.Slides[0]
rect = RectangleF.FromLTRB(0, 0, ppt.SlideSize.Size.Width, ppt.SlideSize.Size.Height)
slide.Shapes.AppendEmbedImageByPath(ShapeType.Rectangle, "bg.png", rect)
slide.Shapes[0].Line.FillFormat.SolidFillColor.Color = Color.get_FloralWhite()
# 圆角矩形 + 3D 效果
shape1 = slide.Shapes.AppendShape(ShapeType.RoundCornerRectangle, RectangleF.FromLTRB(150, 150, 300, 300))
shape1.Fill.FillType = FillFormatType.Solid
shape1.Fill.SolidColor.KnownColor = KnownColors.SkyBlue
effect1 = shape1.ThreeD.ShapeThreeD
effect1.PresetMaterial = PresetMaterialType.Powder # 预设材质
effect1.TopBevel.PresetType = BevelPresetType.ArtDeco # 斜角类型
effect1.TopBevel.Height = 4
effect1.TopBevel.Width = 12
effect1.BevelColorMode = BevelColorType.Contour # 斜角颜色模式
effect1.ContourColor.KnownColor = KnownColors.LightBlue
effect1.ContourWidth = 3.5
# 五边形 + 不同 3D 渲染
shape2 = slide.Shapes.AppendShape(ShapeType.Pentagon, RectangleF.FromLTRB(400, 150, 550, 300))
shape2.Fill.FillType = FillFormatType.Solid
shape2.Fill.SolidColor.KnownColor = KnownColors.LightGreen
effect2 = shape2.ThreeD.ShapeThreeD
effect2.PresetMaterial = PresetMaterialType.SoftEdge
effect2.TopBevel.PresetType = BevelPresetType.SoftRound
effect2.TopBevel.Height = 12
effect2.TopBevel.Width = 12
effect2.BevelColorMode = BevelColorType.Contour
effect2.ContourColor.KnownColor = KnownColors.LawnGreen
effect2.ContourWidth = 5
ppt.SaveToFile("Set3DEffectForShape_out.pptx", FileFormat.Pptx2013)
ppt.Dispose()
[/code]
核心属性:PresetMaterial 控制材质类型;TopBevel 控制顶部斜角的形状、高度和宽度;Contour 属性控制轮廓颜色和宽度。
添加阴影效果
阴影可增强形状的层次感:
[code=python]from spire.presentation import *
ppt = Presentation()
shape = ppt.Slides[0].Shapes.AppendShape(
ShapeType.Rectangle,
RectangleF.FromLTRB(100, 100, 300, 200))
shape.Fill.FillType = FillFormatType.Solid
shape.Fill.SolidColor.KnownColor = KnownColors.LightBlue
# 设置预设阴影
shadow = shape.Effect.PresetShadow
shadow.BlurRadius = 10 # 模糊半径
shadow.Distance = 5 # 阴影距离
shadow.Direction = 45 # 阴影方向(角度)
shadow.ColorFormat.Color = Color.get_Gray()
ppt.SaveToFile("SetShadowEffectForShape.pptx", FileFormat.Pptx2013)
ppt.Dispose()
[/code]
BlurRadius、Distance、Direction 分别控制模糊程度、偏移距离和方向角度。
其他形状格式化选项
渐变填充:
[code=python]shape.Fill.FillType = FillFormatType.Gradient
shape.Fill.Gradient.GradientStops.Add(0, Color.get_Blue())
shape.Fill.Gradient.GradientStops.Add(1, Color.get_White())
shape.Fill.Gradient.GradientType = GradientStyleType.Horizontal
[/code]
图案填充:
[code=python]shape.Fill.FillType = FillFormatType.Pattern
shape.Fill.Pattern.PatternType = PatternFillType.DiagonalBrick
shape.Fill.Pattern.ForeColor = Color.get_Red()
shape.Fill.Pattern.BackColor = Color.get_Yellow()
[/code]
图片填充:
[code=python]shape.Fill.FillType = FillFormatType.Picture
shape.Fill.PictureFill.Picture.Url = "texture.jpg"
shape.Fill.PictureFill.FillType = PictureFillType.Stretch
[/code]
注意:使用图片填充时需确保图片路径正确。
实用技巧
批量组合同类形状:遍历幻灯片,收集所有指定类型的形状,再调用 GroupShapes。
[code=python]rectangles = []
for s in slide.Shapes:
if s.ShapeType == ShapeType.Rectangle:
rectangles.append(s)
if len(rectangles) > 1:
slide.GroupShapes(rectangles)
[/code]
通过替代文本查找形状:设置 AlternativeText 属性标记形状后,可在后续操作中快速定位。
[code=python]shape.AlternativeText = "MyCustomShape"
for s in slide.Shapes:
if s.AlternativeText == "MyCustomShape":
s.Fill.SolidColor.KnownColor = KnownColors.Red
[/code]
保护形状不被修改:锁定选择、位置和大小属性。
[code=python]shape.Locking.SelectionProtection = True
shape.Locking.PositionProtection = True
shape.Locking.SizeProtection = True
[/code]
总结
本文展示了使用 Python + Spire.Presentation 自动化处理 PowerPoint 形状布局与格式的核心方法,包括组合/取消组合、3D 效果、阴影、多种填充以及批量操作技巧。这些技术适用于生成标准化模板、复杂图形(如流程图、组织结构图)和个性化演示文稿,能显著节省手工调整的时间。 |