Einführung
Aspose.Slides FOSS for Python gives you full control over the PowerPoint presentation Lebenszyklus Decks von Grund auf erstellen, Diapositive hinzufügen und neu ordnen, sie ausfüllen mit Formen, Tabellen und formatiertem Text und Speicherung des Ergebnisses in PPTX, ODP oder PDF; Das alles geschieht in reinem Python ohne Abhängigkeit von Microsoft Office oder eine externe Binär-Datei.
Die Bibliothek ist auf PyPI als aspose-slides-foss Die Software ist unter der MIT-Lizenz verfügbar. mit ihm pip, importieren und beginnen, Präsentationen in ein paar Zeilen Code zu erstellen. Sie automatisieren die Berichtserstellung, bauen eine Schiebetrakte-Pipeline oder einbinden Präsentations-Ausgabe in einer Webanwendung, die API deckt die gesamte Oberfläche ab: Slide Sammlungen, Master-/Layout-Slides, Formenkollektionen, Textrahmen und Ausfüllformate sowie Ausfuhrmöglichkeiten.
Dieser Beitrag führt Sie durch die Kern-Präsentationsmanagementfunktionen die Klassen und Methoden, die Sie am häufigsten verwenden werden, wenn Sie mit Slides in Python arbeiten.
Was ist darin enthalten?
Präsentation Lebenszyklus
Die Presentation Erstellen Sie ein neues Deck, manipulieren Sie seinen Inhalt., Die Präsentationen unterstützen das Kontext-Manager-Protokoll, So werden Ressourcen automatisch freigegeben.
import io
from aspose.slides_foss import Presentation
from aspose.slides_foss.export import SaveFormat
with Presentation() as pres:
# A new presentation starts with one blank slide
print("Slides:", len(pres.slides)) # 1
# Save to a file
pres.save("output.pptx", SaveFormat.PPTX)
# Or save to a stream
buf = io.BytesIO()
pres.save(buf, SaveFormat.PPTX)
Sammelvorgänge für Diapositive
SlideCollection Sie können die Bilder hinzufügen, einfügt, klonen, entfernen und neu ordnen. Jede Die Operation aktualisiert den internen Index so, dass die Daten in der len(pres.slides) Die Verteilung der Aktuelle Anzahl.
from aspose.slides_foss import Presentation, ShapeType
with Presentation() as pres:
layout = pres.layout_slides[0]
# Add a blank slide using the first layout
pres.slides.add_empty_slide(layout)
# Insert at a specific position
pres.slides.insert_empty_slide(1, layout)
# Clone an existing slide (shapes are copied)
pres.slides[0].shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 50, 200, 100)
pres.slides.add_clone(pres.slides[0])
print("Total slides:", len(pres.slides)) # 4
# Remove by index
pres.slides.remove_at(3)
Formen und AutoFormen
Fügen Sie Rechtecke, Ellipsen, Dreiecke und andere geometrische Formen zu jeder Folie via ShapeCollection.add_auto_shape().Jede Form zeigt Position, Größe und Drehung., und füllen Eigenschaften, die über Speicher-/Wiederaufladungszyklen hinweg bestehen bleiben.
from aspose.slides_foss import Presentation, ShapeType
with Presentation() as pres:
slide = pres.slides[0]
slide.shapes.clear()
rect = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 50, 300, 150)
rect.rotation = 15
ellipse = slide.shapes.add_auto_shape(ShapeType.ELLIPSE, 400, 50, 200, 200)
# Reorder z-index
slide.shapes.reorder(0, ellipse)
print("Front shape:", slide.shapes[0].shape_type) # ELLIPSE
Tabellen und Daten
Erstellen von Datentabellen mit: ShapeCollection.add_table(), Zelltext über TextFrame, Zellen zusammenführen und Grenzformatierung anwenden. Zeilenhöhe und Spalte Breiten entsprechen den Werten, die Sie dem Konstruktor geben.
from aspose.slides_foss import Presentation, FillType
from aspose.slides_foss.drawing import Color
with Presentation() as pres:
slide = pres.slides[0]
slide.shapes.clear()
table = slide.shapes.add_table(50, 50, [150, 150, 150], [40, 40, 40])
table.rows[0][0].text_frame.text = "Name"
table.rows[0][1].text_frame.text = "Role"
table.rows[0][2].text_frame.text = "Team"
# Merge two cells in the second row
table.merge_cells(table.rows[1][0], table.rows[1][1], False)
# Style the header row borders
for col_idx in range(len(table.columns)):
fmt = table.rows[0][col_idx].cell_format
fmt.border_bottom.fill_format.fill_type = FillType.SOLID
fmt.border_bottom.fill_format.solid_fill_color.color = Color.black
fmt.border_bottom.width = 2
Master- und Layout-Slides
Jede Präsentation hat eine Hierarchie von Master- und Layout-Slides. Zugriff auf sie durch Presentation.masters und . Presentation.layout_slides die Anwendung der Richtlinie in Einklang mit den - Das ist das, was ich will.
from aspose.slides_foss import Presentation
with Presentation() as pres:
master = pres.masters[0]
print("Master layouts:", len(master.layout_slides))
# Pick a layout by type
from aspose.slides_foss import SlideLayoutType
title_layout = pres.layout_slides.get_by_type(SlideLayoutType.TITLE)
if title_layout:
pres.slides.add_empty_slide(title_layout)
Dokument-Eigenschaften
Lesen und Schreiben von integrierten Metadaten wie Titel, Autor und Thema über die DocumentProperties Objekt. Eigenschaften bleiben in der gespeicherten Datei erhalten.
from aspose.slides_foss import Presentation
from aspose.slides_foss.export import SaveFormat
with Presentation() as pres:
props = pres.document_properties
props.title = "Quarterly Report"
props.author = "Engineering"
props.subject = "Q1 2026 Results"
pres.save("report.pptx", SaveFormat.PPTX)
Schneller Start
pip install aspose-slides-foss
from aspose.slides_foss import Presentation, ShapeType, FillType, NullableBool
from aspose.slides_foss.drawing import Color
from aspose.slides_foss.export import SaveFormat
with Presentation() as pres:
slide = pres.slides[0]
slide.shapes.clear()
# Add a title shape
title = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 30, 860, 80)
tf = title.add_text_frame("Quarterly Report")
tf.paragraphs[0].portions[0].portion_format.font_bold = NullableBool.TRUE
tf.paragraphs[0].portions[0].portion_format.font_height = 28
# Add a data table
table = slide.shapes.add_table(50, 150, [200, 200, 200], [40, 40, 40])
headers = ["Region", "Revenue", "Growth"]
for i, h in enumerate(headers):
table.rows[0][i].text_frame.text = h
# Fill the title shape background
title.fill_format.fill_type = FillType.SOLID
title.fill_format.solid_fill_color.color = Color.from_argb(255, 0, 51, 102)
# Save as PPTX
pres.save("report.pptx", SaveFormat.PPTX)
Unterstützte Formate
| Format der Datenbank | Erweiterung | Lesen Sie . | Schreiben . |
|---|---|---|---|
| PPTX | .pptx | ✓ | ✓ |
| PPT | .ppt | — | Geplant |
| PPSX | .ppsx | — | Geplant |
| PPTM | .pptm | — | Geplant |
| POTX | .- Das ist nicht wahr. | — | Geplant |
| ODP | .odp (auch als “ODP”) | — | Geplant |
| .pdf (auf Englisch) | — | Geplant | |
| HTML | .html ist ein | — | Geplant |
| HTML5 | .html ist ein | — | Geplant |
| TIFF | .- Ich bin nicht da. | — | Geplant |
| GIF | .Gif | — | Geplant |
| XPS | .xps | — | Geplant |
| FODP | .fodp (Food) | — | Geplant |
| MD | .md | — | Geplant |
Anmerkung: Derzeit wird nur der PPTX-Export implementiert. SaveFormat Die neuen Systeme sind zwar nicht für die Zukunft kompatibel, aber noch nicht funktionsfähig. save() Die Methode erzeugt immer PPTX-Ausgabe, unabhängig vom Formatparameter.
Open Source und Lizenzierung
Aspose.Slides FOSS for Python is released under the MIT license. You can use it in Die Quellcode ist der Source Code von verfügbar auf: GitHub und das Paket wird am PyPI.