Introducere

Aspose.Slides FOSS for Python gives you full control over the PowerPoint presentation ciclul de viață crearea pachetelor de la zero, adăugarea și reordonarea diapozitivelor, completarea acestora cu forme, tabele și text formatat și salvarea rezultatului în PPTX, ODP, PDF sau Toate acestea se întâmplă în Python pur fără dependență de Microsoft Office sau orice sistem binar extern.

Biblioteca este disponibilă pe PyPI ca: aspose-slides-foss sub licenţa MIT. Instalaţi cu ea. pip, importul, și începe construirea prezentări în câteva linii de cod. automatizează generarea de rapoarte, construiește o conducte cu slide-deck sau încorporare ieșire de prezentare într-o aplicație web, API acoperă întreaga suprafață: diapozitiv colectii, diapozitive principale/de layout, colecții de forme, cadre text, formate de completare și opțiunile de export.

Acest post vă prezintă principalele funcții de gestionare a prezentării clasele și metodele pe care le veți folosi cel mai mult atunci când lucrați cu diapozitive în Python.


Ce înseamnă?

Prezentare Ciclul de viață

În ceea ce priveşte Presentation Creează un pachet nou, manipula conţinutul., prezentările suportă protocolul context-manager, Deci resursele sunt eliberate automat.

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)

Operațiuni de colectare a diapozitivelor

SlideCollection permite să adăugați, inserați , clonaţi, eliminați și reordenați diapozitive. fiecare Operațiunea actualizează indexul intern astfel încât: len(pres.slides) reflectă întotdeauna numărarea curentă.

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)

Forme și AutoForme

Adăugați dreptunghiuri, elipses, triunghii și alte forme geometrie la orice diapozitiv prin intermediul ShapeCollection.add_auto_shape().Fiecare formă expune poziţia, mărimea şi rotaţia., și proprietățile de umplere care persistă în ciclurile de salvare/reîncărcare.

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

Tabelul 1

Crearea tabelelor de date cu: ShapeCollection.add_table(), set text celula prin TextFrame, unificarea celulelor și aplicarea formatării marjelor. înălțimi ale rândurilor și coloanelor lăţimi se potrivesc cu valorile pe care le treci la constructor.

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

Slide-uri de master și layout

Fiecare prezentare are o ierarhie de diapozitive master și diapositive de layout. Accesați-le prin intermediul Presentation.masters și de: Presentation.layout_slides să aplice în mod consecvent formatarea peste punte.

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)

Proprietăți ale documentului

Citirea și scrierea metadatele încorporate, cum ar fi titlul, autorul și subiectul prin intermediul DocumentProperties Obiectul. Proprietăţile persistă în fişierul salvat.

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)

Început rapid

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)

Formatele suportate

FormatulExtindereCiteşte.Scrie .
PPTX.pptx
PPT.pptPlanificată
PPSX.ppsxPlanificată
PPTM.pptmPlanificată
POTX.potxPlanificată
ODP.odpPlanificată
PDF.pdfPlanificată
HTML.html (în limba engleză)Planificată
HTML5.html (în limba engleză)Planificată
TIFF.- Nu , nu .Planificată
GIF.gifPlanificată
XPS.xpsPlanificată
FODP.fodpPlanificată
MD.mdPlanificată

Notă: În prezent, este implementată doar exportul PPTX. Există alte valori de format în sistemele de date ale PCP și PSD. SaveFormat În plus, în cazul în care un sistem de securitate este disponibil pentru compatibilitate cu viitorul, nu există încă o soluţie funcţională. save() metoda produce întotdeauna ieșire PPTX, indiferent de parametrul formatului.


Open Source & Licențiere

Aspose.Slides FOSS for Python is released under the MIT license. You can use it in Proiectele personale, academice şi comerciale fără restricţii. disponibilă la: GitHub și pachetul este publicat la data de 15 ianuarie. PyPI.


Cum să începem?