在 Python 中建立與套用 WordArt 效果
概述
WordArt 效果可讓您以填色、外框、陰影、倒影、發光、變形和 3D 格式化來設計文字。本指南說明如何在未安裝 Microsoft Office 的情況下,使用 Aspose.Slides for Python via .NET 在 PowerPoint 簡報中建立和自訂這些效果。
建立簡易 WordArt 範本並套用至文字
以下範例透過設定文字、字型、圖案填色及外框,建立簡單的 WordArt 樣式。
每個範例都會建立一個新簡報並在第一張投影片上加入一個矩形;不需要輸入檔案。第一個範例將文字設定為「Aspose.Slides」。形狀的位置與尺寸以點 (point) 為單位:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 20, 20, 400, 200)
text_frame = auto_shape.text_frame
portion = text_frame.paragraphs[0].portions[0]
portion.text = "Aspose.Slides"
將字型設定為 36 點的 Arial Black,以便更明顯地看到格式化效果:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 20, 20, 400, 200)
portion = auto_shape.text_frame.paragraphs[0].portions[0]
portion.text = "Aspose.Slides"
portion.portion_format.latin_font = slides.FontData("Arial Black")
portion.portion_format.font_height = 36
套用具有深橙色前景和白色背景的 SMALL_GRID 圖案,接著加入寬度為 1 點的黑色文字外框:
import aspose.slides as slides
import aspose.pydrawing as draw
with slides.Presentation() as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 20, 20, 400, 200)
portion = auto_shape.text_frame.paragraphs[0].portions[0]
portion.text = "Aspose.Slides"
portion.portion_format.latin_font = slides.FontData("Arial Black")
portion.portion_format.font_height = 36
portion.portion_format.fill_format.fill_type = slides.FillType.PATTERN
portion.portion_format.fill_format.pattern_format.fore_color.color = draw.Color.dark_orange
portion.portion_format.fill_format.pattern_format.back_color.color = draw.Color.white
portion.portion_format.fill_format.pattern_format.pattern_style = slides.PatternStyle.SMALL_GRID
portion.portion_format.line_format.width = 1
portion.portion_format.line_format.fill_format.fill_type = slides.FillType.SOLID
portion.portion_format.line_format.fill_format.solid_fill_color.color = draw.Color.black
產生的文字如下:

套用其他 WordArt 效果
以下範例示範如何對文字套用陰影、倒影、發光、變形與 3D 效果。
套用外部陰影效果
外部陰影透過在文字後方放置陰影來增加深度。您可以自訂其顏色、方向、距離、模糊半徑、比例與傾斜。
此範例呼叫 enable_outer_shadow_effect 並設定黑色陰影,模糊半徑為 4 點、方向為 230 度、距離為 30 點。比例值 100 會保留陰影大小,水平傾斜則使其傾斜 20 度。Alpha 變換將不透明度設為 32%:
import aspose.slides as slides
import aspose.pydrawing as draw
with slides.Presentation() as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 20, 20, 400, 200)
portion = auto_shape.text_frame.paragraphs[0].portions[0]
portion.text = "Aspose.Slides"
portion.portion_format.latin_font = slides.FontData("Arial Black")
portion.portion_format.font_height = 36
portion.portion_format.effect_format.enable_outer_shadow_effect()
portion.portion_format.effect_format.outer_shadow_effect.shadow_color.color = draw.Color.black
portion.portion_format.effect_format.outer_shadow_effect.scale_horizontal = 100
portion.portion_format.effect_format.outer_shadow_effect.scale_vertical = 100
portion.portion_format.effect_format.outer_shadow_effect.blur_radius = 4
portion.portion_format.effect_format.outer_shadow_effect.direction = 230
portion.portion_format.effect_format.outer_shadow_effect.distance = 30
portion.portion_format.effect_format.outer_shadow_effect.skew_horizontal = 20
portion.portion_format.effect_format.outer_shadow_effect.skew_vertical = 0
portion.portion_format.effect_format.outer_shadow_effect.shadow_color.color_transform.add(slides.ColorTransformOperation.SET_ALPHA, 0.32)
產生的文字如下:

Note
- 當同時使用外部陰影與預設陰影時,僅套用外部陰影。
- 若同時使用外部陰影與內部陰影,產生的效果取決於 PowerPoint 版本。例如,在 PowerPoint 2013 中,效果會加倍;而在 PowerPoint 2007 中,僅套用外部陰影。
套用倒影效果
倒影會產生文字的鏡像副本。可調整其位置、比例、模糊與不透明度,以控制外觀。
此範例呼叫 enable_reflection_effect 並以 -100% 的比例將倒影垂直翻轉。使用 0.5 點的模糊半徑與 4.72 點的距離。倒影的透明度在 0% 到 60% 位置之間由 60% 降至 0.9%:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 20, 20, 400, 200)
portion = auto_shape.text_frame.paragraphs[0].portions[0]
portion.text = "Aspose.Slides"
portion.portion_format.latin_font = slides.FontData("Arial Black")
portion.portion_format.font_height = 36
portion.portion_format.effect_format.enable_reflection_effect()
portion.portion_format.effect_format.reflection_effect.blur_radius = 0.5
portion.portion_format.effect_format.reflection_effect.distance = 4.72
portion.portion_format.effect_format.reflection_effect.start_pos_alpha = 0
portion.portion_format.effect_format.reflection_effect.end_pos_alpha = 60
portion.portion_format.effect_format.reflection_effect.direction = 90
portion.portion_format.effect_format.reflection_effect.scale_horizontal = 100
portion.portion_format.effect_format.reflection_effect.scale_vertical = -100
portion.portion_format.effect_format.reflection_effect.start_reflection_opacity = 60
portion.portion_format.effect_format.reflection_effect.end_reflection_opacity = 0.9
portion.portion_format.effect_format.reflection_effect.rectangle_align = slides.RectangleAlignment.BOTTOM_LEFT
產生的文字如下:

如需圖形比較漸層位置、不透明度值與淡化角度,請參閱 控制倒影淡化。
套用發光效果
發光會在文字周圍產生柔和的彩色外框。可調整其顏色、不透明度與半徑以控制效果。
此範例呼叫 enable_glow_effect 並套用 54% 不透明度、半徑為 7 點的紅色發光:
import aspose.slides as slides
import aspose.pydrawing as draw
with slides.Presentation() as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 20, 20, 400, 200)
portion = auto_shape.text_frame.paragraphs[0].portions[0]
portion.text = "Aspose.Slides"
portion.portion_format.latin_font = slides.FontData("Arial Black")
portion.portion_format.font_height = 36
portion.portion_format.effect_format.enable_glow_effect()
portion.portion_format.effect_format.glow_effect.color.color = draw.Color.red
portion.portion_format.effect_format.glow_effect.color.color_transform.add(slides.ColorTransformOperation.SET_ALPHA, 0.54)
portion.portion_format.effect_format.glow_effect.radius = 7
產生的文字如下:

套用 WordArt 變形
WordArt 變形會彎曲、伸展或扭曲文字區塊。
將 transform 設為 ARCH_UP_POUR 以向上彎曲整個文字框:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 20, 20, 400, 200)
text_frame = auto_shape.text_frame
text_frame.text = "Aspose.Slides"
text_frame.text_frame_format.transform = slides.TextShapeType.ARCH_UP_POUR
產生的文字如下:

Note
Aspose.Slides for Python via .NET 提供一組預定義的 變形類型。套用 3D 效果於形狀與文字
您可以對形狀或其文字套用 3D 效果。斜角、擠壓、光照與相機設定會控制最終外觀。
以下範例使用 ThreeDFormat 為矩形新增圓形斜角、橙色擠壓與深紅色輪廓。斜角尺寸、擠壓高度、輪廓寬度與深度皆以點為單位。塑膠材質、繞 Z 軸旋轉 40 度的平衡光照,以及透視相機定義其外觀:
import aspose.slides as slides
import aspose.pydrawing as draw
with slides.Presentation() as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 20, 20, 400, 200)
auto_shape.text_frame.text = "Aspose.Slides"
auto_shape.three_d_format.bevel_bottom.bevel_type = slides.BevelPresetType.CIRCLE
auto_shape.three_d_format.bevel_bottom.height = 10.5
auto_shape.three_d_format.bevel_bottom.width = 10.5
auto_shape.three_d_format.bevel_top.bevel_type = slides.BevelPresetType.CIRCLE
auto_shape.three_d_format.bevel_top.height = 12.5
auto_shape.three_d_format.bevel_top.width = 11
auto_shape.three_d_format.extrusion_color.color = draw.Color.orange
auto_shape.three_d_format.extrusion_height = 6
auto_shape.three_d_format.contour_color.color = draw.Color.dark_red
auto_shape.three_d_format.contour_width = 1.5
auto_shape.three_d_format.depth = 3
auto_shape.three_d_format.material = slides.MaterialPresetType.PLASTIC
auto_shape.three_d_format.light_rig.direction = slides.LightingDirection.TOP
auto_shape.three_d_format.light_rig.light_type = slides.LightRigPresetType.BALANCED
auto_shape.three_d_format.light_rig.set_rotation(0, 0, 40)
auto_shape.three_d_format.camera.camera_type = slides.CameraPresetType.PERSPECTIVE_CONTRASTING_RIGHT_FACING

此範例透過 TextFrameFormat.three_d_format 為文字套用相似的 3D 格式。較小的斜角塑造字母邊緣,而擠壓與光照則賦予文字深度:
import aspose.slides as slides
import aspose.pydrawing as draw
with slides.Presentation() as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 20, 20, 400, 200)
text_frame = auto_shape.text_frame
text_frame.text = "Aspose.Slides"
text_frame.text_frame_format.three_d_format.bevel_bottom.bevel_type = slides.BevelPresetType.CIRCLE
text_frame.text_frame_format.three_d_format.bevel_bottom.height = 3.5
text_frame.text_frame_format.three_d_format.bevel_bottom.width = 3.5
text_frame.text_frame_format.three_d_format.bevel_top.bevel_type = slides.BevelPresetType.CIRCLE
text_frame.text_frame_format.three_d_format.bevel_top.height = 4
text_frame.text_frame_format.three_d_format.bevel_top.width = 4
text_frame.text_frame_format.three_d_format.extrusion_color.color = draw.Color.orange
text_frame.text_frame_format.three_d_format.extrusion_height = 6
text_frame.text_frame_format.three_d_format.contour_color.color = draw.Color.dark_red
text_frame.text_frame_format.three_d_format.contour_width = 1.5
text_frame.text_frame_format.three_d_format.depth = 3
text_frame.text_frame_format.three_d_format.material = slides.MaterialPresetType.PLASTIC
text_frame.text_frame_format.three_d_format.light_rig.direction = slides.LightingDirection.TOP
text_frame.text_frame_format.three_d_format.light_rig.light_type = slides.LightRigPresetType.BALANCED
text_frame.text_frame_format.three_d_format.light_rig.set_rotation(0, 0, 40)
text_frame.text_frame_format.three_d_format.camera.camera_type = slides.CameraPresetType.PERSPECTIVE_CONTRASTING_RIGHT_FACING

Note
對文字或其形狀套用 3D 效果——以及這些效果之間的相互作用——受到特定規則的管控。請考慮同時包含文字與其所在形狀的場景。3D 效果包含物件的 3D 表示以及它所放置的場景。
- 如果同時為形狀與文字設定了場景,則形狀的場景優先,文字的場景會被忽略。
- 如果形狀沒有自己的場景但具有 3D 表示,則會使用文字的場景。
- 如果形狀根本沒有 3D 效果,則視為平面,且 3D 效果僅套用於文字。
這些行為與 ThreeDFormat.light_rig 和 ThreeDFormat.camera 屬性相關。
若想在保留形狀 3D 格式的同時保持文字平面且易於閱讀,請參閱 在 3D 形狀上保持文字平面 以比較兩種設定並取得完整的 Python 範例。
常見問題
我可以在不同字體或文字系統(例如阿拉伯文、中文)上使用 WordArt 效果嗎?
是的,Aspose.Slides for Python via .NET 支援 Unicode,且可與所有主要字體和文字系統一起使用。無論使用何種語言,都可以套用陰影、填色與外框等 WordArt 效果,但字體的可用性與呈現可能取決於系統字體。
我可以將 WordArt 效果套用於投影片母片元素嗎?
可以,您可以對母片投影片上的形狀套用 WordArt 效果,包括標題佔位符、頁腳或背景文字。對母片版面的變更將會套用到所有相關的投影片上。
WordArt 效果會影響簡報檔案大小嗎?
會稍微增加。陰影、發光與漸層填色等 WordArt 效果會因為額外的格式化資訊而略微增大檔案大小,但差異通常可以忽略不計。
我可以在未儲存簡報的情況下預覽 WordArt 效果的結果嗎?
可以,您可以使用 Slide.get_image 將包含 WordArt 的投影片轉換為影像(例如 PNG、JPEG),或使用 Shape.get_image 轉換單一形狀。這讓您可以在記憶體或螢幕上預覽結果,然後再儲存或匯出完整簡報。