在 Python via Java 中格式化 PowerPoint 形状

介绍

通过调整轮廓、填充和效果来格式化 PowerPoint 中的形状。Aspose.Slides for Python via Java 让您可以以编程方式控制这些设置,包括透明度、旋转和 3D 格式化。

PowerPoint 中的形状格式化选项

Aspose.Slides for Python via Java 提供类和方法,使您能够使用 PowerPoint 中可用的相同选项来格式化形状。

格式线

使用Shape.getLineFormat来控制形状的轮廓。此示例创建一个未填充、蓝色虚线轮廓的矩形。LineFormat.setStyle选择粗细交叉复合线,而LineFormat.setWidth将其宽度设置为 7 磅,且LineFormat.setDashStyle选择虚线模式。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FillType, LineDashStyle, LineStyle, Presentation, SaveFormat, ShapeType
from java.awt import Color

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 150, 75)
    shape.getFillFormat().setFillType(FillType.NoFill)
    shape.getLineFormat().setStyle(LineStyle.ThickThin)
    shape.getLineFormat().setWidth(7)
    shape.getLineFormat().setDashStyle(LineDashStyle.Dash)
    shape.getLineFormat().getFillFormat().setFillType(FillType.Solid)
    shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.BLUE)

    presentation.save("formatted_lines.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

结果:

演示文稿中格式化的线条

设置自定义线条虚线模式

当预定义的线样式不能提供所需的虚线长度和间隙时,请使用LineFormat.setCustomDashPattern。分配模式会自动将线的虚线样式更改为LineDashStyle.Custom。

数组条目在虚线和间隙长度之间交替,先从虚线开始。每个值都是线宽的倍数:在宽度为 3 磅时,[4, 2] 产生 12 磅虚线后跟 6 磅间隙。使用成对的有限、非负值来定义完整的虚线‑间隙段。示例使用正值以保持每段虚线和间隙可见。

此示例创建五条相同长度、平端帽的蓝色线条。前三条在宽度为 3 磅时比较不同模式。第四条以两倍宽度重复第一种模式,虚线和间隙长度均加倍。第五条从自定义模式切换为实线。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FillType, LineArrowheadStyle, LineCapStyle, LineDashStyle, Presentation, SaveFormat, ShapeType, SlideSizeScaleType
from java.awt import Color

presentation = Presentation()
try:
    presentation.getSlideSize().setSize(600, 300, SlideSizeScaleType.DoNotScale)
    slide = presentation.getSlides().get_Item(0)

    patterns = [
        [4, 2],
        [1, 2],
        [6, 2, 1, 2],
        [4, 2],
        [4, 2]
    ]

    for i, pattern in enumerate(patterns):
        line = slide.getShapes().addAutoShape(ShapeType.Line, 50, 50 + i * 50, 500, 0)
        line.getLineFormat().setWidth(6 if i == 3 else 3)
        line.getLineFormat().getFillFormat().setFillType(FillType.Solid)
        color = Color(0, 0, 139)
        line.getLineFormat().getFillFormat().getSolidFillColor().setColor(color)
        line.getLineFormat().setCapStyle(LineCapStyle.Flat)
        line.getLineFormat().setBeginArrowheadStyle(LineArrowheadStyle.None_)
        line.getLineFormat().setEndArrowheadStyle(LineArrowheadStyle.None_)
        line.getLineFormat().setCustomDashPattern(jpype.JArray(jpype.JFloat)(pattern))

    # 将最后一条线恢复为实线样式。
    slide.getShapes().get_Item(4).getLineFormat().setDashStyle(LineDashStyle.Solid)

    presentation.save("custom_dash_patterns.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

若要改回预定义的虚线样式,请将LineDashStyle.Dash传递给LineFormat.setDashStyle。所选样式控制线的外观。

PowerPoint 中的结果,从上到下:{4, 2}, 3 pt;{1, 2}, 3 pt;{6, 2, 1, 2}, 3 pt;{4, 2}, 6 pt;以及Solid, 3 pt。

PowerPoint 中的三个自定义虚线模式、使用第一个模式的更宽线和实线

设置线端帽

使用LineFormat.setCapStyle来设置开放线的端点。LineCapStyle 类提供三种显式端帽样式:

  • Flat:笔画在端点停止。
  • Round:半圆形端帽延伸至端点之外,长度为线宽的一半。
  • Square:方形端帽延伸至端点之外,长度为线宽的一半。

该类还包含 NotDefined,表示未显式分配端帽样式。

端帽不同于箭头(在端点添加符号)和连接(控制线段之间的拐角)。此示例通过LineFormat.setBeginArrowheadStyle和LineFormat.setEndArrowheadStyle禁用箭头,以便仅比较端帽形状。

以下示例创建三条除端帽外完全相同的水平线,宽度为 20 磅。从上到下,它们分别使用平端帽、圆端帽和方端帽。它们的端点具有相同的水平坐标;圆端帽和方端帽在每个端点之外延伸 10 磅。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FillType, LineArrowheadStyle, LineCapStyle, LineDashStyle, Presentation, SaveFormat, ShapeType, SlideSizeScaleType
from java.awt import Color

presentation = Presentation()
try:
    presentation.getSlideSize().setSize(500, 240, SlideSizeScaleType.DoNotScale)
    slide = presentation.getSlides().get_Item(0)

    cap_styles = [LineCapStyle.Flat, LineCapStyle.Round, LineCapStyle.Square]

    for i, cap_style in enumerate(cap_styles):
        line = slide.getShapes().addAutoShape(ShapeType.Line, 100, 50 + i * 70, 300, 0)
        line.getLineFormat().setWidth(20)
        line.getLineFormat().getFillFormat().setFillType(FillType.Solid)
        color = Color(0, 0, 139)
        line.getLineFormat().getFillFormat().getSolidFillColor().setColor(color)
        line.getLineFormat().setDashStyle(LineDashStyle.Solid)
        line.getLineFormat().setCapStyle(cap_style)
        line.getLineFormat().setBeginArrowheadStyle(LineArrowheadStyle.None_)
        line.getLineFormat().setEndArrowheadStyle(LineArrowheadStyle.None_)

    presentation.save("line_caps.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

PowerPoint 中的结果,从上到下:Flat、Round 和 Square。

PowerPoint 中相同粗线的平直、圆形和方形端帽

对形状线应用素描效果

素描效果使形状线看起来像手绘。使用Shape.getLineFormat访问线设置,使用LineFormat.getSketchFormat访问素描设置,并使用SketchFormat.setSketchType从LineSketchType 类中选择值。

以下 Python 代码展示了如何应用LineSketchType.Curved 效果,读取显式分配的值,并使用LineSketchType.None_ 移除该效果:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import LineSketchType, Presentation, ShapeType

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 200, 100)

    sketch_format = shape.getLineFormat().getSketchFormat()
    sketch_format.setSketchType(LineSketchType.Curved)
    explicit_sketch_type = sketch_format.getSketchType()
    print("Explicit sketch type:", explicit_sketch_type)
    sketch_format.setSketchType(LineSketchType.None_)
finally:
    presentation.dispose()

SketchFormat.getSketchType 返回的值代表直接分配给形状的设置。如果线格式可以从主题、母版幻灯片或布局幻灯片继承,请使用LineFormat.getEffective 并从返回的有效线格式中读取素描类型,如下所示。有效值反映在继承解析后实际应用的格式。此示例需要一个已有的演示文稿,其中首张幻灯片至少有一个形状,并打印两个值:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation

presentation = Presentation("presentation.pptx")
try:
    shape = presentation.getSlides().get_Item(0).getShapes().get_Item(0)

    line_format = shape.getLineFormat()
    explicit_sketch_type = line_format.getSketchFormat().getSketchType()
    effective_line_format = line_format.getEffective()
    effective_sketch_type = effective_line_format.getSketchFormat().getSketchType()

    print("Explicit sketch type:", explicit_sketch_type)
    print("Effective sketch type:", effective_sketch_type)
finally:
    presentation.dispose()

格式连接样式

以下是三种连接类型选项:

  • Round
  • Miter
  • Bevel

连接样式控制两个轮廓段在拐角处的连接方式。使用 Round 获得圆角,使用 Miter 获得尖角,使用 Bevel 获得平滑的倒角。

演示文稿中的连接样式

此示例创建三个黑色矩形,蓝色轮廓宽度为 15 磅。它使用LineFormat.setJoinStyle分别将 LineJoinStyle 的斜接、倒角和圆设置为 miter、bevel 和 round,以比较拐角的绘制方式。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FillType, LineJoinStyle, Presentation, SaveFormat, ShapeType
from java.awt import Color

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    miter_shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 20, 20, 150, 75)
    bevel_shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 210, 20, 150, 75)
    round_shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 20, 135, 150, 75)

    miter_shape.getFillFormat().setFillType(FillType.Solid)
    miter_shape.getFillFormat().getSolidFillColor().setColor(Color.BLACK)
    bevel_shape.getFillFormat().setFillType(FillType.Solid)
    bevel_shape.getFillFormat().getSolidFillColor().setColor(Color.BLACK)
    round_shape.getFillFormat().setFillType(FillType.Solid)
    round_shape.getFillFormat().getSolidFillColor().setColor(Color.BLACK)

    miter_shape.getLineFormat().setWidth(15)
    bevel_shape.getLineFormat().setWidth(15)
    round_shape.getLineFormat().setWidth(15)

    miter_shape.getLineFormat().getFillFormat().setFillType(FillType.Solid)
    miter_shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.BLUE)
    bevel_shape.getLineFormat().getFillFormat().setFillType(FillType.Solid)
    bevel_shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.BLUE)
    round_shape.getLineFormat().getFillFormat().setFillType(FillType.Solid)
    round_shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.BLUE)

    miter_shape.getLineFormat().setJoinStyle(LineJoinStyle.Miter)
    bevel_shape.getLineFormat().setJoinStyle(LineJoinStyle.Bevel)
    round_shape.getLineFormat().setJoinStyle(LineJoinStyle.Round)

    miter_shape.getTextFrame().setText("Miter Join Style")
    bevel_shape.getTextFrame().setText("Bevel Join Style")
    round_shape.getTextFrame().setText("Round Join Style")

    presentation.save("join_styles.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

限制锐角处的斜接连接

将LineFormat.setMiterLimit 与LineFormat.setJoinStyle 设置为LineJoinStyle.Miter 一起使用,以限制锐角可以延伸的距离。

限制是连接的内外角之间距离与线宽的比率。例如,数值 8 允许斜接长度最高为线宽的八倍;这不是以点为单位的测量。如果拐角超过该限制,PowerPoint 会在该拐角使用倒角连接。更大的限制会产生更尖锐的点。此设置仅影响斜接连接,当拐角仍在两者限制范围内时更改它不会产生可见效果。

此示例创建两个相同的未填充三角形,轮廓宽度为 20 磅。左侧三角形使用限制 2,右侧使用 8。每个三角形宽 80 磅、高 160 磅,使其顶部拐角的斜接长度与线宽比约为 4.12。因此,PowerPoint 在左侧顶点使用倒角,而保留右侧顶点的尖角。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FillType, LineJoinStyle, Presentation, SaveFormat, ShapeType, SlideSizeScaleType
from java.awt import Color

presentation = Presentation()
try:
    presentation.getSlideSize().setSize(500, 320, SlideSizeScaleType.DoNotScale)
    slide = presentation.getSlides().get_Item(0)

    miter_limits = [2.0, 8.0]

    for i, miter_limit in enumerate(miter_limits):
        triangle = slide.getShapes().addAutoShape(ShapeType.Triangle, 90 + i * 240, 110, 80, 160)
        triangle.getFillFormat().setFillType(FillType.NoFill)
        triangle.getLineFormat().setWidth(20)
        triangle.getLineFormat().getFillFormat().setFillType(FillType.Solid)
        color = Color(0, 0, 139)
        triangle.getLineFormat().getFillFormat().getSolidFillColor().setColor(color)
        triangle.getLineFormat().setJoinStyle(LineJoinStyle.Miter)
        triangle.getLineFormat().setMiterLimit(miter_limit)

    presentation.save("miter_limits.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

PowerPoint 中的结果:左侧为 Miter limit 2,右侧为 miter limit 8。

PowerPoint 中的斜接限制 2 的倒角顶点和斜接限制 8 的尖锐顶点

渐变填充

渐变填充在形状内部混合颜色。此示例使用FillFormat.getGradientFormat 以 45 度角从红色到紫色为椭圆填充线性渐变。[GradientStopCollection.addPresetColor](https://reference.aspose.com/slides/python-java/aspose.slides/gradientstopcollection/#addPresetColor) 方法在 0 和 1 处放置颜色停止点,即渐变的起始和结束。清除现有的停止点可确保仅使用这两种颜色。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FillType, GradientShape, Presentation, PresetColor, SaveFormat, ShapeType
from java.awt import Color

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addAutoShape(ShapeType.Ellipse, 50, 50, 150, 75)
    shape.getFillFormat().setFillType(FillType.Gradient)
    shape.getFillFormat().getGradientFormat().setGradientShape(GradientShape.Linear)
    shape.getFillFormat().getGradientFormat().setLinearGradientAngle(45)
    shape.getFillFormat().getGradientFormat().getGradientStops().clear()
    shape.getFillFormat().getGradientFormat().getGradientStops().addPresetColor(1.0, PresetColor.Purple)
    shape.getFillFormat().getGradientFormat().getGradientStops().addPresetColor(0.0, PresetColor.Red)

    presentation.save("gradient_fill.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

带有渐变填充的椭圆

如果在导出图像中的渐变与 PowerPoint 中的显示不同,请参阅Choose a Gradient Rendering Style。您可以在不更改演示文稿中存储的渐变填充的情况下选择渲染样式。

设置线性渐变角度

使用GradientFormat.setLinearGradientAngle来控制GradientShape.Linear 填充的方向。角度以度数计,顺时针增加:0 从左到右,90 从上到下,180 从右到左,270 从下到上。支持小数角度。超出 [0, 360) 的有限角度会规范化为一次完整旋转;例如,-45 变为 315,360 变为 0。

此示例创建六个矩形,红、绿、蓝三种停止颜色分别位于 0、0.5、1 处。禁用缩放以便在不受矩形比例影响的情况下比较角度。左到右的角度分别为第一行的 0、45、90,第二行的 135、180、270。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FillType, GradientShape, NullableBool, Presentation, SaveFormat, ShapeType, SlideSizeScaleType
from java.awt import Color

presentation = Presentation()
try:
    presentation.getSlideSize().setSize(720, 400, SlideSizeScaleType.DoNotScale)
    slide = presentation.getSlides().get_Item(0)

    angles = [0, 45, 90, 135, 180, 270]

    for i, angle in enumerate(angles):
        x = 30 + (i % 3) * 230
        y = 30 + (i // 3) * 190

        shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, x, y, 200, 150)
        shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill)
        shape.getFillFormat().setFillType(FillType.Gradient)

        gradient = shape.getFillFormat().getGradientFormat()
        gradient.setGradientShape(GradientShape.Linear)
        gradient.getGradientStops().clear()
        gradient.getGradientStops().add(0, Color.RED)
        gradient.getGradientStops().add(0.5, Color.GREEN)
        gradient.getGradientStops().add(1, Color.BLUE)
        gradient.setLinearGradientScaled(NullableBool.False_)
        gradient.setLinearGradientAngle(angle)

    presentation.save("linear_gradient_angles.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

PowerPoint 中的结果:

顶部行的 0、45、90 度线性渐变以及底部行的 135、180、270 度线性渐变

使用形状缩放线性渐变

GradientFormat.setLinearGradientScaled 接受一个 NullableBool 值。显式设为 False_ 可在形状比例变化时保持几何渐变角度不变,设为 True_ 则随形状宽高比例缩放渐变。启用缩放后,在非正方形形状上可见角度可能与分配的角度不同。

下面的示例使用 45 度角和与角度比较相同的颜色停止点。左列使用 False_;右列使用 True_。每列包含一个宽矩形上方一个高矩形。两者起始宽度相同;通过改变第二个矩形的宽度和高度展示填充在不更改渐变设置的情况下对尺寸调整的响应。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FillType, GradientShape, NullableBool, Presentation, SaveFormat, ShapeType, SlideSizeScaleType
from java.awt import Color

presentation = Presentation()
try:
    presentation.getSlideSize().setSize(720, 460, SlideSizeScaleType.DoNotScale)
    slide = presentation.getSlides().get_Item(0)

    scaling_options = [NullableBool.False_, NullableBool.True_]

    for column, scaled in enumerate(scaling_options):
        for row in range(2):
            x = 30 + column * 360
            y = 30 if row == 0 else 150

            shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, x, y, 300, 90)
            shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill)
            shape.getFillFormat().setFillType(FillType.Gradient)

            gradient = shape.getFillFormat().getGradientFormat()
            gradient.setGradientShape(GradientShape.Linear)
            gradient.getGradientStops().clear()
            gradient.getGradientStops().add(0, Color.RED)
            gradient.getGradientStops().add(0.5, Color.GREEN)
            gradient.getGradientStops().add(1, Color.BLUE)
            gradient.setLinearGradientAngle(45)
            gradient.setLinearGradientScaled(scaled)

            if row == 1:
                shape.setWidth(90)
                shape.setHeight(280)

    presentation.save("linear_gradient_scaling.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

PowerPoint 中的结果,左侧禁用缩放,右侧启用缩放:

宽矩形和高矩形的 45 度渐变,对比左侧 LinearGradientScaled 为 False 与右侧 True

禁用缩放时,对角线颜色带在两个矩形中保持相同方向。启用缩放时,颜色带随形状比例拉伸。这有助于在将形状适配不同大小时复现模板的渐变。对正方形而言,启用缩放不会改变此 45 度渐变的外观。

角度和缩放设置均适用于线性渐变,不会影响 GradientShape.Radial 或 GradientShape.Rectangle 填充。

将角度与渐变方向结合

GradientFormat.setGradientDirection 从 GradientDirection 类中选择渐变样式;它不是数值角度的替代品。对于线性渐变,角落样式可以在不更改通过 setLinearGradientAngle 分配的角度的情况下改变颜色分布的区域。比较角度或缩放时请保持方向不变。

下面的示例以相反顺序应用相同设置。两个矩形的外观相同:先设置方向后设置角度或先设置角度后设置方向均不改变结果。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FillType, GradientDirection, GradientShape, NullableBool, Presentation, SaveFormat, ShapeType
from java.awt import Color

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    for i in range(2):
        shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 30, 30 + i * 140, 320, 100)
        shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill)
        shape.getFillFormat().setFillType(FillType.Gradient)

        gradient = shape.getFillFormat().getGradientFormat()
        gradient.setGradientShape(GradientShape.Linear)
        gradient.getGradientStops().clear()
        gradient.getGradientStops().add(0, Color.RED)
        gradient.getGradientStops().add(0.5, Color.GREEN)
        gradient.getGradientStops().add(1, Color.BLUE)
        gradient.setLinearGradientScaled(NullableBool.True_)

        if i == 0:
            gradient.setGradientDirection(GradientDirection.FromCorner1)
            gradient.setLinearGradientAngle(30)
        else:
            gradient.setLinearGradientAngle(30)
            gradient.setGradientDirection(GradientDirection.FromCorner1)

    presentation.save("linear_gradient_direction.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

图案填充

图案填充使用两色设计,如点、条纹或格子。此示例使用PatternFormat.setPatternStyle选择格子图案。PatternFormat.getBackColor 提供浅灰色背景,PatternFormat.getForeColor 提供黄色图案颜色。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FillType, PatternStyle, Presentation, SaveFormat, ShapeType
from java.awt import Color

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 150, 75)
    shape.getFillFormat().setFillType(FillType.Pattern)
    shape.getFillFormat().getPatternFormat().setPatternStyle(PatternStyle.Trellis)
    shape.getFillFormat().getPatternFormat().getBackColor().setColor(Color.LIGHT_GRAY)
    shape.getFillFormat().getPatternFormat().getForeColor().setColor(Color.YELLOW)

    presentation.save("pattern_fill.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

带图案填充的矩形

图片填充

图片填充在形状内部使用图像。此示例需要本地图像,并使用Images.fromFile 将其加载到演示文稿的图像集合中。将PictureFillFormat.setPictureFillMode 设置为平铺模式可在矩形内重复图像。下面显示的源图像如下:

莲花图片

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FillType, Images, PictureFillMode, Presentation, SaveFormat, ShapeType

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 255, 130)
    shape.getFillFormat().setFillType(FillType.Picture)
    shape.getFillFormat().getPictureFillFormat().setPictureFillMode(PictureFillMode.Tile)

    image = Images.fromFile("lotus.png")
    try:
        picture = presentation.getImages().addImage(image)
    finally:
        image.dispose()
    shape.getFillFormat().getPictureFillFormat().getPicture().setImage(picture)

    presentation.save("picture_fill.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

带图片填充的形状

将图片平铺为纹理

如果想将平铺图片设为纹理并自定义平铺行为,可使用以下 PictureFillFormat 类的方法:

  • setPictureFillMode:设置图片填充模式——Tile 或 Stretch。
  • setTileAlignment:指定平铺在形状内部的对齐方式。
  • setTileFlip:控制平铺是否水平、垂直或双向翻转。
  • setTileOffsetX:设置平铺相对于形状原点的水平偏移(单位:磅)。
  • setTileOffsetY:设置平铺相对于形状原点的垂直偏移(单位:磅)。
  • setTileScaleX:以百分比定义平铺的水平缩放比例。
  • setTileScaleY:以百分比定义平铺的垂直缩放比例。

此示例需要本地图像,并将 PictureFillFormat 配置为以原始大小的 50% 重复。-32 磅的偏移使平铺在水平和垂直方向上移动;右下对齐并在两个方向上翻转决定了重复纹理的排列方式。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FillType, Images, PictureFillMode, Presentation, RectangleAlignment, SaveFormat, ShapeType, TileFlip

presentation = Presentation()
try:
    first_slide = presentation.getSlides().get_Item(0)

    shape = first_slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 190, 95)
    shape.getFillFormat().setFillType(FillType.Picture)

    source_image = Images.fromFile("lotus.png")
    try:
        presentation_image = presentation.getImages().addImage(source_image)
    finally:
        source_image.dispose()

    picture_fill_format = shape.getFillFormat().getPictureFillFormat()
    picture_fill_format.getPicture().setImage(presentation_image)
    picture_fill_format.setPictureFillMode(PictureFillMode.Tile)
    picture_fill_format.setTileOffsetX(-32)
    picture_fill_format.setTileOffsetY(-32)
    picture_fill_format.setTileScaleX(50)
    picture_fill_format.setTileScaleY(50)
    picture_fill_format.setTileAlignment(RectangleAlignment.BottomRight)
    picture_fill_format.setTileFlip(TileFlip.FlipBoth)

    presentation.save("tile.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

平铺选项

纯色填充

纯色填充为形状提供单一、均匀的颜色。此示例使用FillFormat.setFillType 设置为纯色填充模式,并通过FillFormat.getSolidFillColor 赋予黄色,然后将矩形保存到演示文稿中。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FillType, Presentation, SaveFormat, ShapeType
from java.awt import Color

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 150, 75)
    shape.getFillFormat().setFillType(FillType.Solid)
    shape.getFillFormat().getSolidFillColor().setColor(Color.YELLOW)

    presentation.save("solid_color_fill.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

带纯色填充的形状

复制颜色同时保留主题引用

使用ColorFormat.copyFrom 可复制颜色定义,包括其颜色类型、主题引用和转换。这在将填充颜色在应继续遵循演示文稿主题的形状之间转移时非常有用。该方法仅复制颜色本身;它不会复制填充类型、轮廓或其他形状格式。请使用FillFormat.setFillType 显式设置目标的填充类型。

此示例创建三个矩形,左到右分别为:源、主题感知的复制和固定 RGB 复制。源使用ColorFormat.setSchemeColor 引用第一强调色,并通过ColorOperationCollection.add 添加色调。RGB 复制使用源的有效填充颜色,因此最初三个矩形看起来相同。调用ColorFormat.setColor 设置固定的 RGB 值并清除任何现有的颜色转换。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import ColorTransformOperation, FillType, ImageFormat, Presentation, SaveFormat, SchemeColor, ShapeType, SlideSizeScaleType
from java.awt import Color

presentation = Presentation()
try:
    presentation.getSlideSize().setSize(660, 220, SlideSizeScaleType.DoNotScale)
    slide = presentation.getSlides().get_Item(0)

    accent_color = Color(65, 105, 225)
    presentation.getMasterTheme().getColorScheme().getAccent1().setColor(accent_color)

    source = slide.getShapes().addAutoShape(ShapeType.Rectangle, 30, 40, 180, 140)
    theme_copy = slide.getShapes().addAutoShape(ShapeType.Rectangle, 240, 40, 180, 140)
    rgb_copy = slide.getShapes().addAutoShape(ShapeType.Rectangle, 450, 40, 180, 140)

    for shape in (source, theme_copy, rgb_copy):
        shape.getFillFormat().setFillType(FillType.Solid)
        shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill)

    source_color = source.getFillFormat().getSolidFillColor()
    source_color.setSchemeColor(SchemeColor.Accent1)
    source_color.getColorTransform().add(ColorTransformOperation.Tint, 0.75)

    theme_copy.getFillFormat().getSolidFillColor().copyFrom(source_color)
    rgb_copy.getFillFormat().getSolidFillColor().setColor(source.getFillFormat().getEffective().getSolidFillColor())

    before = slide.getImage(1.0, 1.0)
    try:
        before.save("color-copy-before.png", ImageFormat.Png)
    finally:
        before.dispose()

    # 主题引用遵循新的强调色;固定的 RGB 值则不受影响。
    accent_color = Color(255, 69, 0)
    presentation.getMasterTheme().getColorScheme().getAccent1().setColor(accent_color)

    after = slide.getImage(1.0, 1.0)
    try:
        after.save("color-copy-after.png", ImageFormat.Png)
    finally:
        after.dispose()
    presentation.save("copied_colors.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

在主题更改之前,所有三种填充均匹配:

源、copyFrom 目标和固定 RGB 目标的相同蓝色填充

主题更改后,源和主题感知的复制遵循新的强调色并保留其色调。固定的 RGB 复制仍保持蓝色:

源和 copyFrom 目标为橙色填充,而固定 RGB 目标保持蓝色

复制的定义独立于源。随后更改源的主题引用、分配其他颜色或添加/删除其转换都不会影响目标。主题更改仍会影响两个形状,因为它们各自独立地引用相同的主题颜色。

保存并重新打开 PPTX 时,会保留通过ColorFormat.getColorType 返回的颜色类型、主题引用以及通过ColorFormat.getColorTransform 返回的转换集合;不会将定义扁平化为 RGB。

在不同主题环境之间复制时,例如具有不同强调色调色板的演示文稿,复制的引用会依据目标的主题进行解析。因此相同的引用和转换可能产生不同的可见颜色。有关配置主题颜色的更多信息,请参阅Presentation Theme。

使用幻灯片背景作为形状填充

当形状应匹配幻灯片背景时(例如在演示文稿模板中),使用AutoShape.setUseBackgroundFill。首先通过FillFormat.setFillType 将填充类型设为FillType.NotDefined 以移除任何显式填充。这样组合后,形状将使用幻灯片背景而不是其样式填充。仅启用该标志并不会移除已有的显式填充。

这与FillType.NoFill 不同:无填充会让形状后面的对象保持可见,而背景填充会用对应的幻灯片背景区域覆盖形状后面的对象。梯度仍然相对于幻灯片对齐,而不是在每个形状内部重新开始。

以下示例创建一个由母版提供的背景,幻灯片继承该背景,并在三个矩形后放置一条品红色带。左侧矩形使用背景填充,中间矩形无填充,右侧矩形从背景填充切换为显式橙色填充。

import jpide
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import BackgroundType, FillType, GradientShape, Presentation, SaveFormat, ShapeType, SlideSizeScaleType
from java.awt import Color

presentation = Presentation()
try:
    presentation.getSlideSize().setSize(720, 400, SlideSizeScaleType.DoNotScale)
    slide = presentation.getSlides().get_Item(0)

    # 从母版继承渐变。
    slide.getBackground().setType(BackgroundType.NotDefined)
    slide.getLayoutSlide().getBackground().setType(BackgroundType.NotDefined)
    background = slide.getLayoutSlide().getMasterSlide().getBackground()
    background.setType(BackgroundType.OwnBackground)
    background.getFillFormat().setFillType(FillType.Gradient)
    gradient = background.getFillFormat().getGradientFormat()
    gradient.setGradientShape(GradientShape.Linear)
    gradient.setLinearGradientAngle(0)
    gradient.getGradientStops().clear()
    color = Color(0, 0, 139)
    gradient.getGradientStops().add(0, color)
    color = Color(135, 206, 250)
    gradient.getGradientStops().add(1, color)

    # 先添加此对象,使其出现在矩形后面。
    band = slide.getShapes().addAutoShape(ShapeType.Rectangle, 25, 150, 670, 100)
    band.getFillFormat().setFillType(FillType.Solid)
    band.getFillFormat().getSolidFillColor().setColor(Color.MAGENTA)
    band.getLineFormat().getFillFormat().setFillType(FillType.NoFill)

    background_shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 100, 160, 200)
    background_shape.getFillFormat().setFillType(FillType.NotDefined)
    background_shape.setUseBackgroundFill(True)

    no_fill_shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 280, 100, 160, 200)
    no_fill_shape.getFillFormat().setFillType(FillType.NoFill)

    explicit_shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 510, 100, 160, 200)
    explicit_shape.getFillFormat().setFillType(FillType.NotDefined)
    explicit_shape.setUseBackgroundFill(True)

    # 在应用显式填充之前禁用背景填充。
    explicit_shape.setUseBackgroundFill(False)
    explicit_shape.getFillFormat().setFillType(FillType.Solid)
    color = Color(255, 165, 0)
    explicit_shape.getFillFormat().getSolidFillColor().setColor(color)

    presentation.save("background_fill.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

左到右的结果:背景填充、无填充 和 显式填充。品红色带在左、右矩形下被遮挡,但通过中间矩形仍可见。

背景填充、无填充和显式橙色填充覆盖品红色带

相同的行为适用于直接在幻灯片上定义的背景或从布局继承的背景。形状使用已解析的幻灯片背景;本示例保留幻灯片和布局背景未定义,以便使用母版提供的背景。

设置透明度

对于纯色填充,颜色的 alpha 分量控制不透明度:0 表示完全透明,255 表示完全不透明。此示例为黄色矩形使用 alpha 值 128,使其约为 50% 透明,从而下面的矩形仍可见。颜色通过FillFormat.getSolidFillColor 进行分配。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FillType, Presentation, SaveFormat, ShapeType
from java.awt import Color

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    solid_shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 150, 75)

    transparent_shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 80, 80, 150, 75)
    transparent_shape.getFillFormat().setFillType(FillType.Solid)
    color = Color(255, 255, 0, 128)
    transparent_shape.getFillFormat().getSolidFillColor().setColor(color)

    presentation.save("shape_transparency.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

透明形状

旋转形状

使用Shape.setRotation 可在幻灯片平面上旋转形状。角度以度数计;此示例将矩形顺时针旋转 5 度并保存演示文稿。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, SaveFormat, ShapeType

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 150, 75)
    shape.setRotation(5)

    presentation.save("shape_rotation.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

形状旋转

添加 3D 倒角效果

使用Shape.getThreeDFormat 为形状添加深度和倒角边缘。此示例创建一个绿色椭圆并加橙色轮廓,使用ThreeDFormat.setDepth 将深度设为 4 磅,并通过ThreeDFormat.getBevelTop 以宽高各 6 磅的圆形顶部倒角进行设置。前视正交相机和三点光源使倒角可见。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import BevelPresetType, CameraPresetType, FillType, LightRigPresetType, LightingDirection, Presentation, SaveFormat, ShapeType
from java.awt import Color

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addAutoShape(ShapeType.Ellipse, 50, 50, 100, 100)
    shape.getFillFormat().setFillType(FillType.Solid)
    color = Color(0, 128, 0)
    shape.getFillFormat().getSolidFillColor().setColor(color)
    shape.getLineFormat().getFillFormat().setFillType(FillType.Solid)
    color = Color(255, 165, 0)
    shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(color)
    shape.getLineFormat().setWidth(2.0)
    shape.getThreeDFormat().setDepth(4)
    shape.getThreeDFormat().getBevelTop().setBevelType(BevelPresetType.Circle)
    shape.getThreeDFormat().getBevelTop().setHeight(6)
    shape.getThreeDFormat().getBevelTop().setWidth(6)
    shape.getThreeDFormat().getCamera().setCameraType(CameraPresetType.OrthographicFront)
    shape.getThreeDFormat().getLightRig().setLightType(LightRigPresetType.ThreePt)
    shape.getThreeDFormat().getLightRig().setDirection(LightingDirection.Top)

    presentation.save("3D_bevel_effect.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

3D 倒角效果

添加 3D 旋转效果

形状的 3D 视图由相机设置控制。此示例使用Camera.setRotation 将纬度、经度和旋转角度分别设为 40、35、20 度。通过Camera.setCameraType 选择等轴相机,并通过LightRig.setLightType 选择平衡光源,然后保存已格式化的矩形。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import CameraPresetType, LightRigPresetType, Presentation, SaveFormat, ShapeType

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    auto_shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 150, 75)
    auto_shape.getTextFrame().setText("Hello, Aspose!")
    auto_shape.getThreeDFormat().getCamera().setCameraType(CameraPresetType.IsometricLeftUp)
    auto_shape.getThreeDFormat().getCamera().setRotation(40, 35, 20)
    auto_shape.getThreeDFormat().getLightRig().setLightType(LightRigPresetType.Balanced)

    presentation.save("3D_rotation_effect.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

3D 旋转效果

控制形状的黑白渲染

Shape.setBlackWhiteMode 方法指定在以黑白模式查看或处理演示文稿时单个形状的渲染方式。它本身并不会启用黑白显示,也不会在正常颜色模式下更改形状的填充、线条或其他格式。

使用BlackWhiteMode 类中的值来选择所需行为。例如,Automatic 让渲染应用自行决定转换方式,Gray 和 LightGray 使用灰度,BlackWhite 仅使用黑白,Black 和 White 强制单色,Color 保持正常颜色,Hidden 在黑白模式下省略形状。NotDefined 表示未为形状级别分配模式。

此示例创建一个橙色矩形,并使用Shape.setBlackWhiteMode 将模式设为灰色。保存的形状在正常颜色显示下保持橙色填充,在遵循黑白显示设置的应用程序中请求灰色渲染:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import BlackWhiteMode, FillType, Presentation, SaveFormat, ShapeType
from java.awt import Color

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 200, 100)
    shape.getFillFormat().setFillType(FillType.Solid)
    color = Color(255, 165, 0)
    shape.getFillFormat().getSolidFillColor().setColor(color)
    shape.setBlackWhiteMode(BlackWhiteMode.Gray)

    presentation.save("shape_black_white_mode.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

在正常颜色模式下,矩形保持橙色填充。在黑白显示工作流中,由于模式设置为 Gray,它使用灰色渲染。这使您能够在保留全彩幻灯片的同时,为打印、预览或其他遵循黑白显示设置的工作流定义不同的外观。

重置格式

使用Slide.reset 可将幻灯片占位符的位置、大小和格式恢复为其对应布局占位符的设置。此示例需要一个已有布局占位符的演示文稿,并在保存前重置每张幻灯片。没有对应布局占位符的形状不会被重置。

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, SaveFormat

presentation = Presentation("sample.pptx")
try:
    for slide in presentation.getSlides():
        slide.reset()

    presentation.save("reset_formatting.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

常见问题解答

形状格式化会影响最终演示文稿文件大小吗?

颜色、线型和效果设置相对于嵌入的图像和媒体来说通常只会增加少量数据。图片填充可能会增加文件大小,因为其图像会存储在演示文稿中。

如何检测幻灯片上共享相同格式的形状,以便将它们分组?

比较每个形状的关键格式属性——填充、线条和效果设置。如果所有对应值匹配,则视为样式相同,可在逻辑上将这些形状分组,从而简化后续的样式管理。

我可以将一组自定义形状样式保存到单独的文件,以便在其他演示文稿中复用吗?

可以。将带有所需样式的示例形状存储在模板幻灯片组或 .POTX 模板文件中。创建新演示文稿时,打开模板,克隆所需的样式形状,并在需要的地方重新应用其格式。