在 Python 中管理 PowerPoint 演示文稿主题
概述
演示文稿主题定义其设计元素的属性。选择主题时,即是选择一套协调的视觉元素及其属性。
在 PowerPoint 中,主题包括颜色、字体、背景样式和效果。

更改主题颜色
PowerPoint 主题为幻灯片上的不同元素使用一组特定颜色。如果不喜欢默认颜色,可以通过应用新的主题颜色来更改。为方便选择新主题颜色,Aspose.Slides 在 SchemeColor 枚举中提供相应值。
下面的 Python 代码展示了如何更改主题的强调颜色:
import aspose.pydrawing as draw
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 10, 10, 100, 100)
shape.fill_format.fill_type = slides.FillType.SOLID
shape.fill_format.solid_fill_color.scheme_color = slides.SchemeColor.ACCENT4
您可以按如下方式确定生成颜色的实际值:
fill_effective = shape.fill_format.get_effective()
print("{0} ({1})".format(fill_effective.solid_fill_color.name, fill_effective.solid_fill_color))
# 示例输出:
#
# ff8064a2 (颜色 [A=255, R=128, G=100, B=162])
为了进一步演示颜色更改,我们创建另一个元素,将其分配为上一步的强调颜色,然后更新主题颜色。
other_shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 10, 120, 100, 100)
other_shape.fill_format.fill_type = slides.FillType.SOLID
other_shape.fill_format.solid_fill_color.scheme_color = slides.SchemeColor.ACCENT4
presentation.master_theme.color_scheme.accent4.color = draw.Color.red
新颜色会自动应用到两个元素上。
从附加调色板设置主题颜色
当对主主题颜色 (1) 应用亮度变换时,会生成来自附加调色板 (2) 的颜色。随后可以设置并检索这些主题颜色。

1 — 主主题颜色
2 — 来自附加调色板的颜色
以下 Python 代码演示了如何从主主题颜色推导出附加调色板颜色并在形状中使用:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
# 强调色 4
shape1 = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 10, 10, 50, 50)
shape1.fill_format.fill_type = slides.FillType.SOLID
shape1.fill_format.solid_fill_color.scheme_color = slides.SchemeColor.ACCENT4
# 强调色 4,亮度 80%
shape2 = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 10, 70, 50, 50)
shape2.fill_format.fill_type = slides.FillType.SOLID
shape2.fill_format.solid_fill_color.scheme_color = slides.SchemeColor.ACCENT4
shape2.fill_format.solid_fill_color.color_transform.add(slides.ColorTransformOperation.MULTIPLY_LUMINANCE, 0.2)
shape2.fill_format.solid_fill_color.color_transform.add(slides.ColorTransformOperation.ADD_LUMINANCE, 0.8)
# 强调色 4,亮度 60%
shape3 = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 10, 130, 50, 50)
shape3.fill_format.fill_type = slides.FillType.SOLID
shape3.fill_format.solid_fill_color.scheme_color = slides.SchemeColor.ACCENT4
shape3.fill_format.solid_fill_color.color_transform.add(slides.ColorTransformOperation.MULTIPLY_LUMINANCE, 0.4)
shape3.fill_format.solid_fill_color.color_transform.add(slides.ColorTransformOperation.ADD_LUMINANCE, 0.6)
# 强调色 4,亮度 40%
shape4 = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 10, 190, 50, 50)
shape4.fill_format.fill_type = slides.FillType.SOLID
shape4.fill_format.solid_fill_color.scheme_color = slides.SchemeColor.ACCENT4
shape4.fill_format.solid_fill_color.color_transform.add(slides.ColorTransformOperation.MULTIPLY_LUMINANCE, 0.6)
shape4.fill_format.solid_fill_color.color_transform.add(slides.ColorTransformOperation.ADD_LUMINANCE, 0.4)
# 强调色 4,暗度 25%
shape5 = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 10, 250, 50, 50)
shape5.fill_format.fill_type = slides.FillType.SOLID
shape5.fill_format.solid_fill_color.scheme_color = slides.SchemeColor.ACCENT4
shape5.fill_format.solid_fill_color.color_transform.add(slides.ColorTransformOperation.MULTIPLY_LUMINANCE, 0.75)
# 强调色 4,暗度 50%
shape6 = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 10, 310, 50, 50)
shape6.fill_format.fill_type = slides.FillType.SOLID
shape6.fill_format.solid_fill_color.scheme_color = slides.SchemeColor.ACCENT4
shape6.fill_format.solid_fill_color.color_transform.add(slides.ColorTransformOperation.MULTIPLY_LUMINANCE, 0.5)
presentation.save("example.pptx", slides.export.SaveFormat.PPTX)
将 SchemeColor 映射到 ColorScheme 颜色
当使用 SchemeColor 时,您可能会注意到它包含以下主题颜色值:BACKGROUND1、BACKGROUND2、TEXT1 和 TEXT2。
然而,Presentation.master_theme.color_scheme 返回 ColorScheme,它以以下方式公开相应的颜色:dark1、dark2、light1 和 light2。
此差异仅在于命名。这些值指向相同的主题颜色插槽,映射是固定的:
TEXT1=dark1BACKGROUND1=light1TEXT2=dark2BACKGROUND2=light2
TEXT/BACKGROUND 与 dark/light 之间不存在动态转换,它们仅是相同主题颜色的别名。
这种命名差异来源于 Microsoft Office 的术语。较早的 Office 版本使用 Dark 1、Light 1、Dark 2、Light 2,而较新的 UI 版本则将相同的插槽显示为 Text 1、Background 1、Text 2、Background 2。
更改主题字体
为了让您选择主题字体及其他用途,Aspose.Slides 使用了以下特殊标识符(与 PowerPoint 相似):
- +mn-lt — 正文字体 Latin(次要 Latin 字体)
- +mj-lt — 标题字体 Latin(主要 Latin 字体)
- +mn-ea — 正文字体 East Asian(次要东亚字体)
- +mj-ea — 标题字体 East Asian(主要东亚字体)
下面的 Python 代码展示了如何将 Latin 字体分配给主题元素:
portion = slides.Portion("Theme text format")
portion.portion_format.latin_font = slides.FontData("+mn-lt")
paragraph = slides.Paragraph()
paragraph.portions.add(portion)
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 10, 10, 100, 100)
shape.text_frame.paragraphs.add(paragraph)
下面的 Python 示例演示了如何更改演示文稿的主题字体:
presentation.master_theme.font_scheme.minor.latin_font = slides.FontData("Arial")
所有文本框都会更新为新字体。
TIP
欲了解更多信息,请参阅 使用 Python 的 PowerPoint 主字体。更改主题背景样式
默认情况下,PowerPoint 提供 12 种预定义背景,但典型的演示文稿通常只存储其中的 3 种。

例如,在 PowerPoint 中保存演示文稿后,您可以运行以下 Python 代码来确定其中包含了多少预定义背景:
with slides.Presentation() as presentation:
number_of_background_fills = len(presentation.master_theme.format_scheme.background_fill_styles)
print(f"Number of theme background fill styles: {number_of_background_fills}")
background_fill_styles 属性,您可以在 PowerPoint 主题中添加或访问背景样式。
以下 Python 示例展示了如何设置演示文稿的背景:
presentation.masters[0].background.style_index = 2 # 0 表示无填充;索引从 1 开始。
TIP
欲了解更多信息,请参阅 在 Python 中管理演示文稿背景。更改主题效果
PowerPoint 主题通常在每个样式数组中包含三个值。这些数组组合为三种效果级别:细微、适中和强烈。例如,以下是将这些效果应用于特定形状时的结果:

通过使用来自 FormatScheme 类的三个属性——FillStyles、LineStyles 和 EffectStyles——您可以修改主题元素(比在 PowerPoint 中更灵活)。
以下 Python 代码展示了如何通过修改这些元素的部分属性来更改主题效果:
with slides.Presentation("sample.pptx") as presentation:
presentation.master_theme.format_scheme.line_styles[0].fill_format.solid_fill_color.color = draw.Color.red
presentation.master_theme.format_scheme.fill_styles[2].fill_type = slides.FillType.SOLID
presentation.master_theme.format_scheme.fill_styles[2].solid_fill_color.color = draw.Color.forest_green
presentation.master_theme.format_scheme.effect_styles[2].effect_format.outer_shadow_effect.distance = 10
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
产生的更改包括填充颜色、填充类型、阴影效果及其他属性的更新:

常见问题
我可以在不更改母版的情况下将主题应用于单个幻灯片吗?
可以。Aspose.Slides 支持幻灯片级别的主题覆盖,您可以仅在该幻灯片上应用本地主题,同时保持母版主题不变(通过 SlideThemeManager)。
将主题从一个演示文稿安全地迁移到另一个演示文稿的最佳方式是什么?
将 克隆幻灯片 与其母版一起复制到目标演示文稿中。这会保留原始母版、布局以及相关主题,从而保持外观一致。
如何查看所有继承和覆盖之后的“实际”值?
使用 API 的“实际”视图(针对主题、颜色、字体、效果)。这些视图在应用母版以及任何本地覆盖后返回解析后的最终属性。