Wie man in Python zu PowerPoint Formen hinzufügt

Wie man in Python zu PowerPoint Formen hinzufügt

Aspose.Slides FOSS for Python supports adding AutoShapes, Tables, Connectors, and PictureFrames to presentation slides. All shape types are added through the slide.shapes Sammlung.

Schritt für Schritte

Schritt 1: Installieren des Pakets

pip install aspose-slides-foss

Die Installation überprüfen:

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

Schritt 2: Erstellen Sie eine Präsentation

Verwenden Sie immer: Presentation als Kontextmanager.

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)

Schritt 3: Ein AutoShape hinzufügen

slide.shapes.add_auto_shape(shape_type, x, y, width, height) Die Verwendung von “System” ist nicht unbedingt erforderlich. ShapeType Konstanten zur Auswahl der Form.

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)

Schritt 4: Ein Tabelle hinzufügen

slide.shapes.add_table(x, y, col_widths, row_heights) Die Spaltenbreiten und Zeilenhöhen sind Listen von Punktwerten.

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)

Schritt 5: Hinzufügen eines Anschlusses

Die Anschlüsse verbinden zwei Formen visuell. Erstellen Sie die Forme zuerst, fügen Sie dann einen Ansprechpartner hinzu und setzen Sie seine Start- und Endverbindungspunkte fest.

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)

Die Verbindungsstandortindizes werden für ein Rechteck mit der Nummer 03 nummeriert: oben = 0, links = 1, unten = 2, rechts = 3.


Schritt 6: Ein Bildrahmen hinzufügen

Einbinden eines Bildes und fügen Sie es als ein Slide hinzu. PictureFrame.Lesen Sie die Bildbyte zuerst, fügen sie in die Bilderkollektion der Präsentation hinzu und erstellen Sie dann den Rahmen.

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)

Häufige Probleme und Lösungen

Form erscheint außerhalb des sichtbaren Schiebetreffs.

Die Vorgabe für die Bilder ist 960 × 540 Punkte. x oder y Über diese Grenzen hinaus, legen Sie die Form abseits der Folie. x < 960 und y < 540, und gewährleisten x + width <= 960 und y + height <= 540.

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

add_auto_shape() Wir sehen, dass die Form des Objekts direkt zurückgegeben wird. None, überprüfen Sie, dass Sie den Rückgabewert nicht verwerfen.

Tabellenzellentext nach Zuweisung leer .

Die richtige Eigenschaft ist .text_frame.text (nicht .text Zugriff auf Zellen als table.rows[row_index][col_index].text_frame.text = "value".


Häufig gestellte Fragen

Wie viele Formen kann ich einer Folie hinzufügen?

Es gibt keine Bibliotheksgrenze. Praktische Grenzen hängen von der Dateigröße und den Rendering-Fähigkeiten des Ziel-PPTX Viewers ab.

Kann ich die Position einer Form ändern, nachdem ich sie hinzugefügt habe?

Ja, das von add_auto_shape() hat x, y, width, und height Eigenschaften, die Sie einstellen können:

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

Kann ich die Farbe der Form umrissen?

Ja, über die 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)

Werden Diagramme unterstützt?

Nein. Diagramme, SmartArt und OLE-Objekte werden in dieser Ausgabe nicht implementiert und erhöhen die NotImplementedError.


Siehe auch:

 Deutsch