Crea e applica effetti WordArt in Python via Java

Panoramica

Le effetti WordArt consentono di aggiungere testo stilizzato e visivamente attraente alle presentazioni PowerPoint. Con Aspose.Slides, gli sviluppatori possono creare, personalizzare e gestire programmaticamente WordArt proprio come in Microsoft PowerPoint, senza la necessità di avere Office installato. Questo articolo fornisce una panoramica sull’utilizzo di WordArt, includendo come applicare trasformazioni di testo, stili di riempimento, contorni, ombre e altre opzioni di formattazione per rendere il contenuto della presentazione più espressivo e coinvolgente. WordArt consente di trattare il testo come un oggetto grafico. È costituito da effetti o modifiche speciali applicate al testo per renderlo più attraente o evidente.

Crea un modello WordArt semplice e applicalo al testo

Utilizzando Aspose.Slides

Per prima cosa, creiamo del testo semplice usando questo codice Python:

import jpype
import asposeslides

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

from asposeslides.api import Presentation, ShapeType

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)
    auto_shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200)
    text_frame = auto_shape.getTextFrame()

    portion = text_frame.getParagraphs().get_Item(0).getPortions().get_Item(0)
    portion.setText("Aspose.Slides")
finally:
    presentation.dispose()

Successivamente, aumentiamo la dimensione del carattere per rendere l’effetto più evidente:

import jpype
import asposeslides

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

from asposeslides.api import FontData, Presentation, ShapeType

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)
    auto_shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200)
    text_frame = auto_shape.getTextFrame()
    portion = text_frame.getParagraphs().get_Item(0).getPortions().get_Item(0)
    portion.setText("Aspose.Slides")

    font_data = FontData("Arial Black")
    portion_format = portion.getPortionFormat()
    portion_format.setLatinFont(font_data)
    portion_format.setFontHeight(36)
finally:
    presentation.dispose()

Utilizzando Microsoft PowerPoint

Vai al menu degli effetti WordArt in Microsoft PowerPoint:

WordArt effects menu in PowerPoint

Dalla barra laterale destra, è possibile scegliere un effetto WordArt predefinito. Dalla barra laterale sinistra, è possibile specificare le impostazioni per un nuovo WordArt.

Ecco alcuni dei parametri o opzioni disponibili:

WordArt formatting options

Utilizzando Aspose.Slides

Qui applichiamo il riempimento a pattern PatternStyle.SmallGrid al testo e aggiungiamo un bordo testuale nero usando questo codice:

import jpype
import asposeslides

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

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

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)
    auto_shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200)
    text_frame = auto_shape.getTextFrame()
    portion = text_frame.getParagraphs().get_Item(0).getPortions().get_Item(0)
    portion.setText("Aspose.Slides")

    portion_format = portion.getPortionFormat()
    portion_format.getFillFormat().setFillType(FillType.Pattern)
    pattern_format = portion_format.getFillFormat().getPatternFormat()
    pattern_format.getForeColor().setColor(Color.ORANGE)
    pattern_format.getBackColor().setColor(Color.WHITE)
    pattern_format.setPatternStyle(PatternStyle.SmallGrid)

    line_format = portion_format.getLineFormat()
    line_format.getFillFormat().setFillType(FillType.Solid)
    line_format.getFillFormat().getSolidFillColor().setColor(Color.BLACK)
finally:
    presentation.dispose()

Il testo risultante:

Text with a pattern fill and black outline

Applicare altri effetti WordArt

Utilizzando Microsoft PowerPoint

Dall’interfaccia del programma, è possibile applicare questi effetti a testo, blocco di testo, forma o un elemento simile:

Text and shape effects in PowerPoint

Ad esempio, gli effetti Ombra, Riflesso e Bagliore possono essere applicati al testo; gli effetti Formato 3D e Rotazione 3D possono essere applicati a un blocco di testo; l’effetto Bordi morbidi può essere applicato a una forma (ha comunque effetto quando non è impostato alcun effetto Formato 3D).

Applicare effetti Ombra

Il seguente codice Python applica un effetto ombra solo al testo:

import jpype
import asposeslides

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

from asposeslides.api import ColorTransformOperation, Presentation, ShapeType
from java.awt import Color

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)
    auto_shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200)
    text_frame = auto_shape.getTextFrame()
    portion = text_frame.getParagraphs().get_Item(0).getPortions().get_Item(0)
    portion.setText("Aspose.Slides")

    portion_format = portion.getPortionFormat()
    portion_format.getEffectFormat().enableOuterShadowEffect()
    outer_shadow = portion_format.getEffectFormat().getOuterShadowEffect()
    outer_shadow.getShadowColor().setColor(Color.BLACK)
    outer_shadow.setScaleHorizontal(100)
    outer_shadow.setScaleVertical(65)
    outer_shadow.setBlurRadius(4.73)
    outer_shadow.setDirection(230)
    outer_shadow.setDistance(2)
    outer_shadow.setSkewHorizontal(30)
    outer_shadow.setSkewVertical(0)
    outer_shadow.getShadowColor().getColorTransform().add(ColorTransformOperation.SetAlpha, 0.32)
finally:
    presentation.dispose()

L’API Aspose.Slides supporta tre tipi di ombre: OuterShadow, InnerShadow, e PresetShadow.

Con PresetShadow, è possibile applicare un’ombra al testo utilizzando valori predefiniti.

Utilizzando Microsoft PowerPoint

In PowerPoint, è possibile utilizzare un solo tipo di ombra. Ecco un esempio:

Shadow settings in PowerPoint

Utilizzando Aspose.Slides

Aspose.Slides consente effettivamente di applicare due tipi di ombre contemporaneamente: InnerShadow e PresetShadow.

Note:

  • Quando OuterShadow e PresetShadow sono utilizzati insieme, viene applicato solo l’effetto OuterShadow.
  • Se OuterShadow e InnerShadow sono usati simultaneamente, l’effetto risultante o applicato dipende dalla versione di PowerPoint. Ad esempio, in PowerPoint 2013, l’effetto è duplicato. Invece, in PowerPoint 2007, viene applicato l’effetto OuterShadow.

Applicare riflessione al testo

Aggiungiamo una riflessione al testo tramite questo esempio di codice in Python via Java:

import jpype
import asposeslides

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

from asposeslides.api import Presentation, RectangleAlignment, ShapeType

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)
    auto_shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200)
    text_frame = auto_shape.getTextFrame()
    portion = text_frame.getParagraphs().get_Item(0).getPortions().get_Item(0)
    portion.setText("Aspose.Slides")

    portion_format = portion.getPortionFormat()
    portion_format.getEffectFormat().enableReflectionEffect()
    reflection = portion_format.getEffectFormat().getReflectionEffect()
    reflection.setBlurRadius(0.5)
    reflection.setDistance(4.72)
    reflection.setStartPosAlpha(0)
    reflection.setEndPosAlpha(60)
    reflection.setDirection(90)
    reflection.setScaleHorizontal(100)
    reflection.setScaleVertical(-100)
    reflection.setStartReflectionOpacity(60)
    reflection.setEndReflectionOpacity(0.9)
    reflection.setRectangleAlign(RectangleAlignment.BottomLeft)
finally:
    presentation.dispose()

Applicare un effetto Bagliore al testo

Applichiamo l’effetto bagliore al testo per farlo brillare o risaltare usando questo codice:

import jpype
import asposeslides

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

from asposeslides.api import ColorTransformOperation, Presentation, ShapeType

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)
    auto_shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200)
    text_frame = auto_shape.getTextFrame()
    portion = text_frame.getParagraphs().get_Item(0).getPortions().get_Item(0)
    portion.setText("Aspose.Slides")

    portion_format = portion.getPortionFormat()
    portion_format.getEffectFormat().enableGlowEffect()
    glow = portion_format.getEffectFormat().getGlowEffect()
    glow.getColor().setR(jpype.JByte(-1))
    glow.getColor().getColorTransform().add(ColorTransformOperation.SetAlpha, 0.54)
    glow.setRadius(7)
finally:
    presentation.dispose()

Il risultato dell’operazione:

Text with a glow effect

Utilizzare le trasformazioni in WordArt

Utilizza TextFrameFormat.setTransform per trasformare l’intero blocco di testo:

import jpime
import asposeslides

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

from asposeslides.api import Presentation, ShapeType, TextShapeType

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)
    auto_shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200)
    text_frame = auto_shape.getTextFrame()
    text_frame.setText("Aspose.Slides")

    text_frame.getTextFrameFormat().setTransform(TextShapeType.ArchUpPour)
finally:
    presentation.dispose()

Il risultato:

Text with an arch transformation

Utilizzando PowerPoint

Per accedere ai tipi di trasformazione predefiniti, vai su: Formato -> EffettoTesto -> Trasforma

Utilizzando Aspose.Slides

Per selezionare un tipo di trasformazione, utilizza l’enumerazione TextShapeType.

Applicare effetti 3D a testo e forme

Applichiamo un effetto 3D a una forma di testo usando questo esempio di codice:

import jpype
import asposeslides

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

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

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)
    auto_shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200)
    auto_shape.getTextFrame().setText("Aspose.Slides")

    three_d_format = auto_shape.getThreeDFormat()
    three_d_format.getBevelBottom().setBevelType(BevelPresetType.Circle)
    three_d_format.getBevelBottom().setHeight(10.5)
    three_d_format.getBevelBottom().setWidth(10.5)

    three_d_format.getBevelTop().setBevelType(BevelPresetType.Circle)
    three_d_format.getBevelTop().setHeight(12.5)
    three_d_format.getBevelTop().setWidth(11)

    three_d_format.getExtrusionColor().setColor(Color.ORANGE)
    three_d_format.setExtrusionHeight(6)

    three_d_format.getContourColor().setColor(Color.RED)
    three_d_format.setContourWidth(1.5)

    three_d_format.setDepth(3)

    three_d_format.setMaterial(MaterialPresetType.Plastic)

    three_d_format.getLightRig().setDirection(LightingDirection.Top)
    three_d_format.getLightRig().setLightType(LightRigPresetType.Balanced)
    three_d_format.getLightRig().setRotation(0, 0, 40)

    three_d_format.getCamera().setCameraType(CameraPresetType.PerspectiveContrastingRightFacing)
finally:
    presentation.dispose()

Il testo risultante e la sua forma:

Text shape with 3D effects

Applichiamo un effetto 3D al testo con questo codice Python:

import jpype
import asposeslides

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

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

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)
    auto_shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200)
    text_frame = auto_shape.getTextFrame()
    text_frame.setText("Aspose.Slides")

    three_d_format = text_frame.getTextFrameFormat().getThreeDFormat()
    three_d_format.getBevelBottom().setBevelType(BevelPresetType.Circle)
    three_d_format.getBevelBottom().setHeight(3.5)
    three_d_format.getBevelBottom().setWidth(3.5)

    three_d_format.getBevelTop().setBevelType(BevelPresetType.Circle)
    three_d_format.getBevelTop().setHeight(4)
    three_d_format.getBevelTop().setWidth(4)

    three_d_format.getExtrusionColor().setColor(Color.ORANGE)
    three_d_format.setExtrusionHeight(6)

    three_d_format.getContourColor().setColor(Color.RED)
    three_d_format.setContourWidth(1.5)

    three_d_format.setDepth(3)

    three_d_format.setMaterial(MaterialPresetType.Plastic)

    three_d_format.getLightRig().setDirection(LightingDirection.Top)
    three_d_format.getLightRig().setLightType(LightRigPresetType.Balanced)
    three_d_format.getLightRig().setRotation(0, 0, 40)

    three_d_format.getCamera().setCameraType(CameraPresetType.PerspectiveContrastingRightFacing)
finally:
    presentation.dispose()

Il risultato dell’operazione:

Text with 3D effects

Applicare effetti Ombra esterna al testo

Aspose.Slides per Python via Java fornisce le classi OuterShadow e InnerShadow che consentono di applicare effetti di ombra al testo in un TextFrame. Segui questi passaggi:

  1. Crea un’istanza della classe Presentation .
  2. Ottieni il riferimento a una diapositiva usando il suo indice.
  3. Aggiungi una forma rettangolare alla diapositiva.
  4. Accedi al frame di testo associato alla forma.
  5. Disattiva il riempimento della forma.
  6. Abilita l’effetto ombra esterna.
  7. Imposta il raggio di sfocatura dell’ombra.
  8. Imposta la direzione dell’ombra.
  9. Imposta la distanza dell’ombra.
  10. Allinea l’ombra in alto a sinistra.
  11. Imposta il colore dell’ombra su nero.
  12. Salva la presentazione come file PPTX .

Questo codice di esempio in Python via Java — un’implementazione dei passaggi sopra — mostra come applicare l’effetto ombra esterna al testo:

import jpype
import asposeslides

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

from asposeslides.api import FillType, Presentation, PresetColor, RectangleAlignment, SaveFormat, ShapeType

presentation = Presentation()
try:
    # Ottieni il riferimento della diapositiva
    slide = presentation.getSlides().get_Item(0)

    # Aggiungi un AutoShape di tipo Rettangolo
    auto_shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 150, 75, 150, 50)

    # Aggiungi TextFrame al Rettangolo
    auto_shape.addTextFrame("Aspose TextBox")

    # Disattiva il riempimento della forma nel caso volessimo ottenere l'ombra del testo
    auto_shape.getFillFormat().setFillType(FillType.NoFill)

    # Aggiungi ombra esterna e imposta tutti i parametri necessari
    auto_shape.getEffectFormat().enableOuterShadowEffect()
    shadow = auto_shape.getEffectFormat().getOuterShadowEffect()
    shadow.setBlurRadius(4.0)
    shadow.setDirection(45)
    shadow.setDistance(3)
    shadow.setRectangleAlign(RectangleAlignment.TopLeft)
    shadow.getShadowColor().setPresetColor(PresetColor.Black)

    # Scrivi la presentazione su disco
    presentation.save("pres_out.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

Applicare effetto Ombra interna alle forme

Segui questi passaggi:

  1. Crea un’istanza della classe Presentation .
  2. Ottieni un riferimento alla diapositiva.
  3. Aggiungi una forma rettangolare.
  4. Abilita l’effetto ombra interna.
  5. Imposta tutti i parametri necessari.
  6. Imposta il tipo di colore dell’ombra per utilizzare un colore del tema.
  7. Imposta il colore del tema.
  8. Salva la presentazione come file PPTX .

Questo codice di esempio (basato sui passaggi sopra) mostra come applicare l’effetto ombra interna al testo in una forma in Python via Java:

import jpype
import asposeslides

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

from asposeslides.api import ColorType, FillType, Presentation, SaveFormat, SchemeColor, ShapeType

presentation = Presentation()
try:
    # Ottieni il riferimento della diapositiva
    slide = presentation.getSlides().get_Item(0)

    # Aggiungi un AutoShape di tipo Rectangle
    auto_shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 150, 75, 400, 300)
    auto_shape.getFillFormat().setFillType(FillType.NoFill)

    # Aggiungi TextFrame al Rectangle
    auto_shape.addTextFrame("Aspose TextBox")
    portion = auto_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0)
    portion_format = portion.getPortionFormat()
    portion_format.setFontHeight(50)

    # Abilita InnerShadowEffect
    effect_format = portion_format.getEffectFormat()
    effect_format.enableInnerShadowEffect()

    # Imposta tutti i parametri necessari
    inner_shadow = effect_format.getInnerShadowEffect()
    inner_shadow.setBlurRadius(8.0)
    inner_shadow.setDirection(90.0)
    inner_shadow.setDistance(6.0)
    inner_shadow.getShadowColor().setB(jpype.JByte(-67))

    # Imposta ColorType come Scheme
    inner_shadow.getShadowColor().setColorType(ColorType.Scheme)

    # Imposta Scheme Color
    inner_shadow.getShadowColor().setSchemeColor(SchemeColor.Accent1)

    # Salva la presentazione
    presentation.save("WordArt_out.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

FAQ

Posso utilizzare gli effetti WordArt con diversi caratteri o script (ad esempio, arabo, cinese)?

Sì, Aspose.Slides supporta Unicode e funziona con tutti i principali caratteri e script. Gli effetti WordArt come ombra, riempimento e contorno possono essere applicati indipendentemente dalla lingua, sebbene la disponibilità dei caratteri e il rendering possano dipendere dai caratteri di sistema.

Posso applicare gli effetti WordArt agli elementi del master della diapositiva?

Sì, è possibile applicare gli effetti WordArt alle forme nei master delle diapositive, inclusi i segnaposto del titolo, i piè di pagina o il testo di sfondo. Le modifiche apportate al layout master verranno riflesse in tutte le diapositive associate.

Gli effetti WordArt influiscono sulla dimensione del file della presentazione?

Leggermente. Gli effetti WordArt come ombre, bagliori e riempimenti a gradiente possono aumentare leggermente la dimensione del file a causa dei metadati di formattazione aggiunti, ma la differenza è generalmente trascurabile.

Posso visualizzare in anteprima il risultato degli effetti WordArt senza salvare la presentazione?

Sì, è possibile renderizzare le diapositive contenenti WordArt in immagini (ad esempio PNG, JPEG) utilizzando Shape.getImage o Slide.getImage. Questo consente di visualizzare in anteprima il risultato in memoria o sullo schermo prima di salvare o esportare l’intera presentazione.