Cum să adăugați forme în PowerPoint în Python

Cum să adăugați forme în PowerPoint în Python

Aspose.Slides FOSS for Python acceptă adăugarea AutoShapes, Tables, Connectors și PictureFrames la diapozitivele prezentării. Toate tipurile de forme sunt adăugate prin colecția slide.shapes.

Ghid pas cu pas

Pasul 1: Instalați pachetul

pip install aspose-slides-foss

Verificați instalarea:

import aspose.slides_foss as slides
print("Ready")

Pasul 2: Crează o prezentare

Folosiți întotdeauna Presentation ca manager de context.

import aspose.slides_foss as slides
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]
    # ... add shapes ...
    prs.save("output.pptx", SaveFormat.PPTX)

Pasul 3: Adăugați un AutoShape

slide.shapes.add_auto_shape(shape_type, x, y, width, height) plasează o formă la poziția și dimensiunea date (toate în puncte). Folosiți constantele ShapeType pentru a selecta forma.

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]

    # Rectangle
    rect = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 50, 300, 100)
    rect.add_text_frame("Rectangle shape")

    # Ellipse
    ellipse = slide.shapes.add_auto_shape(ShapeType.ELLIPSE, 400, 50, 200, 100)
    ellipse.add_text_frame("Ellipse shape")

    prs.save("autoshapes.pptx", SaveFormat.PPTX)

Pasul 4: Adaugă un tabel

slide.shapes.add_table(x, y, col_widths, row_heights) creează un tabel la poziția specificată. Lățimile coloanelor și înălțimile rândurilor sunt liste de valori în puncte.

import aspose.slides_foss as slides
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]

    col_widths = [150.0, 150.0, 150.0]
    row_heights = [40.0, 40.0, 40.0]
    table = slide.shapes.add_table(50, 200, col_widths, row_heights)

    # Set header row text
    headers = ["Product", "Units", "Revenue"]
    for col, text in enumerate(headers):
        table.rows[0][col].text_frame.text = text

    # Set data rows
    rows = [
        ["Widget A", "120", "$2,400"],
        ["Widget B", "85", "$1,700"],
    ]
    for row_idx, row_data in enumerate(rows):
        for col, text in enumerate(row_data):
            table.rows[row_idx + 1][col].text_frame.text = text

    prs.save("table.pptx", SaveFormat.PPTX)

Pasul 5: Adaugă un Conector

Conectorii leagă vizual două forme. Creați formele mai întâi, apoi adăugați un conector și setați punctele de conexiune de început și de sfârșit.

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]

    box1 = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 100, 150, 60)
    box1.add_text_frame("Start")

    box2 = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 350, 100, 150, 60)
    box2.add_text_frame("End")

    conn = slide.shapes.add_connector(ShapeType.BENT_CONNECTOR3, 0, 0, 10, 10)
    conn.start_shape_connected_to = box1
    conn.start_shape_connection_site_index = 3  # right side of box1
    conn.end_shape_connected_to = box2
    conn.end_shape_connection_site_index = 1    # left side of box2

    prs.save("connector.pptx", SaveFormat.PPTX)

Indicele site-urilor de conectare sunt numerotate 0–3 pentru un dreptunghi: top=0, left=1, bottom=2, right=3.


Pasul 6: Adaugă un cadru foto

Încorporează o imagine și adaugă‑o pe diapozitiv ca un PictureFrame. Citește mai întâi octeții imaginii, adaugă-i în colecția de imagini a prezentării, apoi creează cadrul.

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    with open("logo.png", "rb") as f:
        image_data = f.read()

    image = prs.images.add_image(image_data)

    slide = prs.slides[0]
    slide.shapes.add_picture_frame(
        ShapeType.RECTANGLE,  # bounding shape type
        50, 50,               # x, y in points
        200, 150,             # width, height in points
        image
    )

    prs.save("with-image.pptx", SaveFormat.PPTX)

Probleme comune și soluții

Forma apare în afara zonei vizibile a diapozitivului

Diapozitivele sunt în mod implicit 720 × 540 puncte. Valorile x sau y dincolo de aceste limite plasează forma în afara diapozitivului. Păstrați x < 720 și y < 540 și asigurați x + width <= 720 și y + height <= 540.

AttributeError: 'NoneType' object has no attribute 'text_frame'

add_auto_shape() returnează direct obiectul shape. Dacă vedeți None, verificați că nu renunțați la valoarea returnată.

Textul celulei din tabel este gol după atribuire

Proprietatea corectă este .text_frame.text (nu .text direct pe celulă). Accesați celulele ca table.rows[row_index][col_index].text_frame.text = "value".


Întrebări frecvente

Câte forme pot adăuga pe un diapozitiv?

Nu există o limită impusă de bibliotecă. Limitele practice depind de dimensiunea fișierului și de capacitatea de redare a vizualizatorului PPTX țintă.

Pot să schimb poziția unei forme după ce am adăugat-o?

Da. Obiectul shape returnat de add_auto_shape() are proprietățile x, y, width și height pe care le puteți seta:

shape.x = 100
shape.y = 200
shape.width = 400
shape.height = 80

Pot să setez culoarea conturului (bordurii) formei?

Da, prin shape.line_format:

from aspose.slides_foss.drawing import Color
shape.line_format.fill_format.solid_fill_color.color = Color.from_argb(255, 200, 0, 0)

Sunt graficele acceptate?

Nu. Graficele, SmartArt și obiectele OLE nu sunt implementate în această ediție și generează NotImplementedError.


Vezi și

 Română