Gestionar gráficos SmartArt en presentaciones usando Python

Resumen

Aspose.Slides le permite crear y administrar gráficos SmartArt en presentaciones de PowerPoint de forma programática. Este artículo explica cómo agregar una forma SmartArt a una diapositiva, acceder a formas SmartArt existentes, encontrar SmartArt por un tipo de diseño específico y actualizar su apariencia visual cambiando el estilo SmartArt o el estilo de color.

Los ejemplos muestran cómo trabajar con formas SmartArt a través de la colección de formas de la diapositiva de la presentación, comprobar si una forma es SmartArt y luego modificar o inspeccionar sus propiedades.

Crear una forma SmartArt

Aspose.Slides for Python via Java provides an API to create SmartArt shapes. To create a SmartArt shape in a slide, please follow the steps below:

  1. Create an instance of the Presentation class.
  2. Get a slide by its index.
  3. Agregar una forma SmartArt by specifying a SmartArtLayoutType.
  4. Save the modified presentation as a PPTX file.
import jpype
import asposeslides

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

from asposeslides.api import Presentation, SaveFormat, SmartArtLayoutType

presentation = Presentation()
try:
    # Obtener la primera diapositiva.
    slide = presentation.getSlides().get_Item(0)

    # Añadir una forma SmartArt.
    smart_art = slide.getShapes().addSmartArt(0, 0, 400, 400, SmartArtLayoutType.BasicBlockList)

    # Guardar la presentación.
    presentation.save("SimpleSmartArt.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()
SmartArt shape
Figura: Forma SmartArt añadida a la diapositiva

Acceder a una forma SmartArt en una diapositiva

The following example accesses SmartArt shapes on a presentation slide. It iterates through every shape on the slide and checks whether the shape is a SmartArt instance.

import jpype
import asposeslides

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

from asposeslides.api import Presentation, SmartArt

presentation = Presentation("AccessSmartArtShape.pptx")
try:
    # Recorrer cada forma en la primera diapositiva.
    for shape in presentation.getSlides().get_Item(0).getShapes():
        if isinstance(shape, SmartArt):
            smart_art = shape
            print("Shape Name: " + str(smart_art.getName()))
finally:
    presentation.dispose()

Acceder a una forma SmartArt con un tipo de diseño particular

The following example accesses a SmartArt shape with a particular layout type, returned by SmartArt.getLayout.

  1. Create an instance of the Presentation class and load the presentation containing a SmartArt shape.
  2. Get the first slide by its index.
  3. Iterate through every shape on the first slide.
  4. Check whether the shape is a SmartArt instance.
  5. Check whether the SmartArt shape has the specified layout type and perform the required operation.
import jpype
import asposeslides

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

from asposeslides.api import Presentation, SmartArt, SmartArtLayoutType

presentation = Presentation("AccessSmartArtShape.pptx")
try:
    # Recorrer cada forma en la primera diapositiva.
    for shape in presentation.getSlides().get_Item(0).getShapes():
        if isinstance(shape, SmartArt):
            smart_art = shape

            # Comprobar el diseño del SmartArt.
            if smart_art.getLayout() == SmartArtLayoutType.BasicBlockList:
                print("Perform the required operation here.")
finally:
    presentation.dispose()

Cambiar el estilo de una forma SmartArt

This example shows how to change the quick style of a SmartArt shape.

  1. Create an instance of the Presentation class and load the presentation containing a SmartArt shape.
  2. Get the first slide by its index.
  3. Iterate through every shape on the first slide.
  4. Check whether the shape is a SmartArt instance.
  5. Find the SmartArt shape with the specified style.
  6. Set the new style for the SmartArt shape.
  7. Save the presentation.
import jpype
import asposeslides

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

from asposeslides.api import Presentation, SaveFormat, SmartArt, SmartArtQuickStyleType

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

    # Recorrer cada forma en la primera diapositiva.
    for shape in slide.getShapes():
        if isinstance(shape, SmartArt):
            smart_art = shape

            # Comprobar y cambiar el estilo del SmartArt.
            if smart_art.getQuickStyle() == SmartArtQuickStyleType.SimpleFill:
                smart_art.setQuickStyle(SmartArtQuickStyleType.Cartoon)

    presentation.save("ChangeSmartArtStyle.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()
SmartArt shape
Figura: Forma SmartArt con estilo cambiado

Cambiar el estilo de color de una forma SmartArt

This example accesses a SmartArt shape with a particular color style and changes that style.

  1. Create an instance of the Presentation class and load the presentation containing a SmartArt shape.
  2. Get the first slide by its index.
  3. Iterate through every shape on the first slide.
  4. Check whether the shape is a SmartArt instance.
  5. Find the SmartArt shape with the specified color style.
  6. Set the new color style for the SmartArt shape.
  7. Save the presentation.
import jpype
import asposeslides

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

from asposeslides.api import Presentation, SaveFormat, SmartArt, SmartArtColorType

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

    # Recorrer cada forma en la primera diapositiva.
    for shape in slide.getShapes():
        if isinstance(shape, SmartArt):
            smart_art = shape

            # Comprobar y cambiar el estilo del SmartArt.
            if smart_art.getColorStyle() == SmartArtColorType.ColoredFillAccent1:
                smart_art.setColorStyle(SmartArtColorType.ColorfulAccentColors)

    presentation.save("ChangeSmartArtColorStyle.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()
SmartArt shape
Figura: Forma SmartArt con estilo de color cambiado

Preguntas frecuentes

¿Puedo animar SmartArt como un solo objeto?

Sí. SmartArt es una forma, por lo que puede aplicar animaciones estándar mediante la API de animaciones (entrada, salida, énfasis, rutas de movimiento) al igual que con otras formas.

¿Cómo puedo encontrar un SmartArt específico en una diapositiva si no conozco su ID interno?

Establezca y use el texto alternativo y busque la forma por ese valor; esta es una forma recomendada de localizar la forma objetivo.

¿Puedo agrupar SmartArt con otras formas?

Sí. Puede agrupar SmartArt con otras formas (imágenes, tablas, etc.) y luego manipular el grupo.

¿Cómo obtengo una imagen de un SmartArt específico (p. ej., para una vista previa o informe)?

Exporte una miniatura/imágen de la forma; la biblioteca puede renderizar formas individuales a archivos raster (PNG/JPG/TIFF).

¿Se preservará la apariencia de SmartArt al convertir toda la presentación a PDF?

Sí. El motor de renderizado apunta a una alta fidelidad para la exportación a PDF, con una variedad de opciones de calidad y compatibilidad.