Introduction
Hướng dẫn này cho thấy cách tạo, sửa đổi và lưu các bản trình bày PowerPoint một cách lập trình trong Python bằng cách sử dụng Aspose.Slides FOSS. Thư viện cho phép bạn xây dựng slides từ scratch, thêm và sắp xếp slids, thu thập chúng với hình dạng, bảng, và định dạng văn bản, and save the result to PPTX. All processing takes place in pure Python with no dependency on Microsoft Office or any external binary.
Thư viện có sẵn trên PyPI như: aspose-slides-foss theo giấy phép MIT. Install Nó với pip, nhập nó, và bắt đầu xây dựng các bản trình bày trong một vài dòng mã. hoặc bạn đang tự động tạo ra báo cáo, xây dựng một đường ống trượt hoặc tích hợp kết quả trình bày trong một ứng dụng web, API bao gồm toàn bộ bề mặt: slide bộ sưu tập, master/layout slides, shape collections, text frames, fill formats, và Các tùy chọn xuất khẩu.
Bài viết này đi qua các khả năng quản lý trình bày chính - những lớp học và phương pháp mà bạn sẽ sử dụng nhiều nhất khi làm việc với slides trong Python.
Những gì được bao gồm
Giới thiệu LIFECYCLE
Các Presentation lớp là điểm nhập. tạo một tấm mới, thao tác nội dung của nó và lưu vào PPTX. sử dụng Presentation Là một nhà quản lý context với with để phát hành file handle tự động.
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)
Slide Collection hoạt động
SlideCollection cho phép bạn thêm, nhập, clone, loại bỏ và đặt lại các slides. len(pres.slides) phản ánh số hiện tại sau mỗi thay đổi.
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)
Hình dạng và AutoShapes
Thêm các góc thẳng, ellipses, triangles và các hình dạng địa phương khác vào bất kỳ slide qua ShapeCollection.add_auto_shape().Mỗi hình dạng thể hiện vị trí, kích thước, xoay, và điền các tài sản mà tồn tại trong vòng lưu / tải lại.
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
Tables
Tạo các bảng dữ liệu với ShapeCollection.add_table(), Set cell text qua TextFrame, kết hợp các tế bào, và áp dụng định dạng biên giới. chiều cao của sợi dây và cột bề rộng phù hợp với các giá trị bạn chuyển cho nhà xây dựng.
from aspose.slides_foss import Presentation, FillType
from aspose.slides_foss.drawing import Color
table = slide.shapes.add_table(50, 50, [150, 150, 150], [40, 40, 40])
# Merge two cells in the second row
table.merge_cells(table.rows[1][0], table.rows[1][1], False)
# Style the header row with a bottom border
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.from_argb(255, 0, 0, 0)
fmt.border_bottom.width = 2
Master & Layout Slides
Mỗi trình bày có một hiến pháp của các slides chủ và layout slids. truy cập chúng qua Presentation.masters và Presentation.layout_slides Sử dụng liên tục định dạng trên các tấm thảm của bạn.
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)
Tài liệu tài sản
Đọc và viết các metadata tích hợp như tiêu đề, tác giả và chủ đề thông qua DocumentProperties Thiết lập bất kỳ tài sản nào trước khi gọi điện thoại save() - chúng được tích hợp trong các metadata PPTX và có thể đọc bằng công cụ downstream:
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)
Khởi động nhanh
Chạy lệnh pip sau để cài đặt Aspose.Slides FOSS for Python từ PyPI:
pip install aspose-slides-foss
Ví dụ sau đây tạo ra một bản trình bày với hình dạng tiêu đề, bảng dữ liệu và một lấp đầy, sau đó lưu nó cho PPTX:
from aspose.slides_foss import Presentation, ShapeType, FillType, NullableBool
from aspose.slides_foss.drawing import Color
from aspose.slides_foss.export import SaveFormat
# 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)
Các định dạng hỗ trợ
| Format | Extension | Read | Write |
|---|---|---|---|
| PPTX | .pptx | ✓ | ✓ |
| PPT | .ppt | — | Planned |
| PPSX | .ppsx | — | Planned |
| PPTM | .pptm | — | Planned |
| POTX | .Đồ họa: Potx | — | Planned |
| ODP | .Đáp: ODP | — | Planned |
| — | Planned | ||
| HTML | .html | — | Planned |
| HTML5 | .html | — | Planned |
| TIFF | .Tiff | — | Planned |
| GIF | .GIF | — | Planned |
| XPS | .XPS | — | Planned |
| FODP | .Phân tích | — | Planned |
| MD | .Md | — | Planned |
Lưu ý : PPTX đọc và viết được thực hiện đầy đủ. khác SaveFormat các giá trị (PPT, ODP, PDF, vv) được lên kế hoạch cho các bản phát hành trong tương lai; chuyển chúng đến save() Hiện tại sản xuất PPTX.
Open Source & Giấy phép
Aspose.Slides FOSS for Python is released under the MIT license. You can use it in các dự án cá nhân, học thuật và thương mại mà không có giới hạn. mã nguồn là: Có sẵn trên GitHub Và gói được công bố trên PyPI.