Gestionar listas con viñetas y numeradas en presentaciones con Python
Visión general
Aspose.Slides para Python mediante .NET le permite crear y dar formato a listas con viñetas y numeradas en presentaciones PowerPoint y OpenDocument. Un elemento de lista es un párrafo cuyas configuraciones de viñeta se controlan a través de su formato de párrafo.
Utilice la Paragraph.paragraph_format propiedad para acceder a la configuración de listas a nivel de párrafo. El punto de entrada principal es ParagraphFormat.bullet, que devuelve un objeto BulletFormat. Con este objeto, puede establecer el tipo de viñeta, símbolo, imagen, color, tamaño, estilo de numeración y número inicial.
Este artículo muestra cómo:
- crear una lista con viñetas con un símbolo personalizado
- crear una viñeta de imagen
- crear una lista multinivel estableciendo la profundidad del párrafo
- crear una lista numerada
- inspeccionar y cambiar el formato de lista en una presentación existente
- leer los marcadores y números reales de la lista junto con el texto del párrafo
Crear una lista con viñetas
Para crear una lista con viñetas, añada objetos Paragraph a un TextFrame, y establezca BulletFormat.type a BulletType.SYMBOL. Luego puede establecer BulletFormat.char, BulletFormat.color, y BulletFormat.height para controlar la apariencia de la viñeta.
El siguiente código Python demuestra cómo crear una lista con viñetas en una diapositiva:
import aspose.slides as slides
def create_paragraph(text):
paragraph = slides.Paragraph()
paragraph.paragraph_format.bullet.type = slides.BulletType.SYMBOL
paragraph.paragraph_format.bullet.char = '*'
paragraph.paragraph_format.indent = 15
paragraph.paragraph_format.bullet.is_bullet_hard_color = slides.NullableBool.TRUE
paragraph.paragraph_format.bullet.color.color = slides.Color.indian_red
paragraph.paragraph_format.bullet.height = 100
paragraph.text = text
return paragraph
with slides.Presentation() as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 20, 20, 200, 50)
text_frame = auto_shape.text_frame
text_frame.paragraphs.clear()
paragraph1 = create_paragraph("The first paragraph")
text_frame.paragraphs.add(paragraph1)
paragraph2 = create_paragraph("The second paragraph")
text_frame.paragraphs.add(paragraph2)
presentation.save("symbol_bullets.pptx", slides.export.SaveFormat.PPTX)
El resultado:

Crear una lista numerada
Utilice listas numeradas cuando el orden de los elementos sea importante. Establezca BulletFormat.type a BulletType.NUMBERED. También puede elegir un formato de numeración con BulletFormat.numbered_bullet_style o establecer BulletFormat.numbered_bullet_start_with cuando la lista debe comenzar desde un valor distinto de 1.
El siguiente código Python muestra cómo crear una lista numerada en una diapositiva:
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, 90, 80)
text_frame = auto_shape.text_frame
text_frame.paragraphs.clear()
paragraph1 = slides.Paragraph()
paragraph1.paragraph_format.bullet.type = slides.BulletType.NUMBERED
paragraph1.text = "Apple"
text_frame.paragraphs.add(paragraph1)
paragraph2 = slides.Paragraph()
paragraph2.paragraph_format.bullet.type = slides.BulletType.NUMBERED
paragraph2.text = "Orange"
text_frame.paragraphs.add(paragraph2)
paragraph3 = slides.Paragraph()
paragraph3.paragraph_format.bullet.type = slides.BulletType.NUMBERED
paragraph3.text = "Banana"
text_frame.paragraphs.add(paragraph3)
presentation.save("numbered_bullets.pptx", slides.export.SaveFormat.PPTX)
El resultado:

Crear una viñeta de imagen
Aspose.Slides le permite reemplazar un símbolo de viñeta regular con una imagen. Las viñetas de imagen funcionan mejor con imágenes simples que siguen siendo legibles en un tamaño pequeño, como iconos o archivos PNG pequeños y transparentes.
Note
Idealmente, si planea reemplazar el símbolo de viñeta regular con una imagen, es mejor elegir un gráfico simple con fondo transparente. Ese tipo de imágenes funciona bien como símbolos de viñeta personalizados.
Tenga en cuenta que la imagen se reducirá a un tamaño muy pequeño. Por esa razón, recomendamos encarecidamente seleccionar una imagen que permanezca clara y visualmente eficaz cuando se use como viñeta en una lista.
Para crear una viñeta de imagen, añada una imagen a Presentation.images y asigne el objeto de imagen devuelto a BulletFormat.picture. Establezca BulletFormat.type a BulletType.PICTURE antes de asignar la imagen.
Supongamos que tenemos un “image.png”:

El siguiente código Python muestra cómo crear viñetas de imagen en una diapositiva:
import aspose.slides as slides
def create_paragraph(text, image):
paragraph = slides.Paragraph()
paragraph.paragraph_format.bullet.type = slides.BulletType.PICTURE
paragraph.paragraph_format.bullet.picture.image = image
paragraph.paragraph_format.indent = 15
paragraph.paragraph_format.bullet.height = 100
paragraph.text = text
return paragraph
with slides.Presentation() as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 20, 20, 200, 50)
text_frame = auto_shape.text_frame
text_frame.paragraphs.clear()
with open("image.png", "rb") as image_stream:
bullet_image = presentation.images.add_image(image_stream)
paragraph1 = create_paragraph("The first paragraph", bullet_image)
text_frame.paragraphs.add(paragraph1)
paragraph2 = create_paragraph("The second paragraph", bullet_image)
text_frame.paragraphs.add(paragraph2)
presentation.save("picture_bullets.pptx", slides.export.SaveFormat.PPTX)
El resultado:

Crear una lista multinivel
Utilice ParagraphFormat.depth para colocar los elementos de lista en diferentes niveles. El nivel 0 es el nivel superior, el nivel 1 está anidado bajo él, y así sucesivamente. La profundidad controla solo la sangría; establezca ParagraphFormat.bullet en cada párrafo cuando los niveles también deben llevar viñetas.
El siguiente código Python muestra cómo crear una lista multinivel:
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, 260, 110)
text_frame = auto_shape.text_frame
text_frame.paragraphs.clear()
paragraph1 = slides.Paragraph()
paragraph1.paragraph_format.depth = 0
paragraph1.text = "My text - Depth 0"
text_frame.paragraphs.add(paragraph1)
paragraph2 = slides.Paragraph()
paragraph2.paragraph_format.depth = 1
paragraph2.text = "My text - Depth 1"
text_frame.paragraphs.add(paragraph2)
paragraph3 = slides.Paragraph()
paragraph3.paragraph_format.depth = 2
paragraph3.text = "My text - Depth 2"
text_frame.paragraphs.add(paragraph3)
paragraph4 = slides.Paragraph()
paragraph4.paragraph_format.depth = 3
paragraph4.text = "My text - Depth 3"
text_frame.paragraphs.add(paragraph4)
presentation.save("multilevel_bullets.pptx", slides.export.SaveFormat.PPTX)
El resultado:

Cambiar una lista existente
Para cambiar el formato de lista en una presentación existente, acceda al párrafo objetivo y actualice sus configuraciones ParagraphFormat.bullet. Las mismas propiedades usadas para crear listas pueden usarse para inspeccionar o modificar listas cargadas desde un archivo PPT, PPTX o ODP.
El siguiente código Python cambia el primer párrafo en un marco de texto para usar un estilo de lista numerada:
import aspose.slides as slides
with slides.Presentation("input.pptx") as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes[0]
paragraph = auto_shape.text_frame.paragraphs[0]
paragraph.paragraph_format.bullet.type = slides.BulletType.NUMBERED
paragraph.paragraph_format.bullet.numbered_bullet_style = slides.NumberedBulletStyle.BULLET_ROMAN_UC_PERIOD
paragraph.paragraph_format.bullet.numbered_bullet_start_with = 1
paragraph.paragraph_format.margin_left = 30
paragraph.paragraph_format.indent = -20
presentation.save("updated_list.pptx", slides.export.SaveFormat.PPTX)
Leer los valores reales de las viñetas
Al extraer el contenido de una presentación, mantenga los marcadores de lista junto al texto del párrafo. Llame a ParagraphFormat.get_effective y acceda a IParagraphFormatEffectiveData.bullet para obtener el formato efectivo de la viñeta. Verifique su type antes de leer actual_bullet_value.
Para listas numeradas, distinga estos tres valores:
- numbered_bullet_style determina el formato, como números arábigos seguidos de un punto o letras minúsculas seguidas de un punto.
- numbered_bullet_start_with especifica el número inicial para un grupo de párrafos numerados.
- actual_bullet_value devuelve el marcador formateado para un párrafo particular. Por ejemplo, el segundo elemento en una lista que comienza en 5 devuelve
6..
Conserve depth en cada párrafo para mantener su nivel de anidamiento. El marcador real no codifica la jerarquía completa: un elemento anidado puede devolver c. en lugar de una etiqueta combinada como 6.c..
El siguiente ejemplo autónomo crea listas, las guarda en una presentación y recarga la presentación para informar el texto, la profundidad, el tipo de viñeta y el valor real de cada párrafo. El pequeño marcador de imagen azul se crea a partir de SVG en línea.
import aspose.slides as slides
with slides.Presentation() as presentation:
presentation.slide_size.set_size(720, 440, slides.SlideSizeScaleType.DO_NOT_SCALE)
slide = presentation.slides[0]
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 30, 20, 660, 400)
shape.fill_format.fill_type = slides.FillType.NO_FILL
shape.line_format.fill_format.fill_type = slides.FillType.NO_FILL
text_frame = shape.text_frame
text_frame.paragraphs.clear()
# Crear un marcador de imagen sin un archivo de imagen externo.
svg = slides.SvgImage("<svg xmlns='http://www.w3.org/2000/svg' width='16' height='16'><rect width='16' height='16' rx='4' fill='#0078D4'/></svg>")
bullet_image = presentation.images.add_image(svg)
def add_paragraph(text, depth, bullet_type, start=1, style=slides.NumberedBulletStyle.BULLET_ARABIC_PERIOD):
paragraph = slides.Paragraph()
paragraph.text = text
paragraph_format = paragraph.paragraph_format
paragraph_format.depth = depth
paragraph_format.margin_left = 30 + depth * 40
paragraph_format.indent = -28
paragraph_format.space_after = 8
paragraph_format.default_portion_format.font_height = 22
paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
paragraph_format.default_portion_format.fill_format.solid_fill_color.color = slides.Color.black
paragraph_format.bullet.type = bullet_type
paragraph_format.bullet.is_bullet_hard_color = slides.NullableBool.TRUE
paragraph_format.bullet.color.color = slides.Color.black
if bullet_type == slides.BulletType.NUMBERED:
paragraph_format.bullet.numbered_bullet_style = style
paragraph_format.bullet.numbered_bullet_start_with = start
elif bullet_type == slides.BulletType.SYMBOL:
paragraph_format.bullet.char = "\u2022"
elif bullet_type == slides.BulletType.PICTURE:
paragraph_format.bullet.picture.image = bullet_image
text_frame.paragraphs.add(paragraph)
add_paragraph("First item", 0, slides.BulletType.NUMBERED, 5)
add_paragraph("Second item", 0, slides.BulletType.NUMBERED, 5)
add_paragraph("Nested item", 1, slides.BulletType.NUMBERED, 3, slides.NumberedBulletStyle.BULLET_ALPHA_LC_PERIOD)
add_paragraph("Next nested item", 1, slides.BulletType.NUMBERED, 3, slides.NumberedBulletStyle.BULLET_ALPHA_LC_PERIOD)
add_paragraph("Deep item", 2, slides.BulletType.NUMBERED, 4, slides.NumberedBulletStyle.BULLET_ROMAN_LC_PERIOD)
add_paragraph("Back to top level", 0, slides.BulletType.NUMBERED, 5)
add_paragraph("Symbol marker", 0, slides.BulletType.SYMBOL)
add_paragraph("No bullet", 0, slides.BulletType.NONE)
add_paragraph("Picture marker", 0, slides.BulletType.PICTURE)
presentation.save("actual_bullet_values.pptx", slides.export.SaveFormat.PPTX)
with slides.Presentation("actual_bullet_values.pptx") as loaded_presentation:
loaded_shape = loaded_presentation.slides[0].shapes[0]
bullet_type_names = {slides.BulletType.NUMBERED: "NUMBERED", slides.BulletType.SYMBOL: "SYMBOL", slides.BulletType.NONE: "NONE", slides.BulletType.PICTURE: "PICTURE"}
print("Text | Depth | Type | Actual value")
for paragraph in loaded_shape.text_frame.paragraphs:
bullet = paragraph.paragraph_format.get_effective().bullet
if bullet.type in (slides.BulletType.NUMBERED, slides.BulletType.SYMBOL):
marker = bullet.actual_bullet_value
elif bullet.type == slides.BulletType.NONE:
marker = f"{bullet.actual_bullet_value} (no bullet)"
elif bullet.type == slides.BulletType.PICTURE:
marker = f"{bullet.actual_bullet_value} (image required)"
else:
marker = "unsupported bullet type"
bullet_type_name = bullet_type_names.get(bullet.type, "UNKNOWN")
print(f"{paragraph.text} | {paragraph.paragraph_format.depth} | {bullet_type_name} | {marker}")
Las listas de origen se muestran a continuación.

El informe coincide con los marcadores mostrados en PowerPoint:
Text | Depth | Type | Actual value
First item | 0 | NUMBERED | 5.
Second item | 0 | NUMBERED | 6.
Nested item | 1 | NUMBERED | c.
Next nested item | 1 | NUMBERED | d.
Deep item | 2 | NUMBERED | iv.
Back to top level | 0 | NUMBERED | 7.
Symbol marker | 0 | SYMBOL | •
No bullet | 0 | NONE | None (no bullet)
Picture marker | 0 | PICTURE | None (image required)
Para BulletType.NONE and BulletType.PICTURE, actual_bullet_value devuelve None, no una cadena vacía. Maneje estos tipos por separado: el primero no tiene marcador, mientras que el segundo requiere extraer la imagen de la viñeta por separado. No se puede reproducir una viñeta de imagen únicamente extrayendo un marcador de texto.
Preguntas frecuentes
¿Se pueden exportar listas con viñetas y numeradas a PDF o imágenes?
Sí. Aspose.Slides conserva el formato de la lista cuando el formato de destino admite la disposición de texto y las características de viñetas correspondientes.
¿Puedo editar listas en presentaciones existentes?
Sí. Cargue la presentación, acceda al párrafo objetivo, inspeccione o actualice sus configuraciones ParagraphFormat.bullet, y guarde la presentación.
¿Pueden las listas contener texto no latino?
Sí. El texto de los elementos de la lista puede contener caracteres Unicode, por lo que puede crear listas en presentaciones multilingües. Asegúrese de que las fuentes utilizadas en la presentación admitan los caracteres que necesita.