1. 产品
  2.   Aspose.Slides
  3.   Aspose.Slides FOSS for Python

Aspose.Slides FOSS 适用于 Python

从 Python 创建、读取和编辑 PowerPoint 演示文稿 — 免费且开源,无需 Office 依赖。

用于 PowerPoint 演示文稿的开源 Python 库

Aspose.Slides FOSS for Python 是一个基于 MIT 许可证的纯 Python 库,用于处理 PowerPoint .pptx 文件。只需一条 pip 命令即可安装,并立即开始创建、读取和编辑演示文稿,无需安装 Microsoft Office 或任何专有运行时。

该库提供了围绕 PresentationSlideShapeTextFrameParagraphPortion 构建的 Presentation API,这些是 PowerPoint 本身使用的概念模型。可以添加和删除幻灯片,插入自动形状、表格和连接线,在字符级别对文本进行加粗、斜体、字号和颜色等格式化,应用纯色或渐变填充,并添加视觉效果(阴影、发光、模糊、反射)。

上下文管理器模式确保可靠的资源清理:始终使用 with slides.Presentation(...) as prs: 打开 Presentation。在加载过程中遇到的未知 XML 部分会在保存时原样保留,因此往返操作永远不会破坏库尚未理解的内容。该库要求 Python 3.10 或更高版本,并且仅依赖 lxml,会自动安装。

Presentation and Slide API

  • Create and open PPTX: Create new presentations or open existing .pptx files.
  • Add and remove slides: Programmatically manage the slide collection.
  • AutoShapes: Insert rectangles, ellipses, lines, and other AutoShape types.
  • Tables and Connectors: Add structured table shapes and connector lines between shapes.
  • Speaker notes: Read and write per-slide speaker notes.
  • Threaded comments: Access slide-level comment threads.

Where Aspose.Slides FOSS Can Be Used

  • Report generation: Build branded slide decks from data sources without Office.
  • Template automation: Fill PPTX templates with dynamic content in CI/CD pipelines.
  • Content migration: Read existing presentations and restructure or re-style slides.
  • Serverless backends: Process PPTX files inside Docker containers or Lambda functions.
  • Batch processing: Apply uniform formatting changes across large slide deck libraries.

Text Formatting and Visual Effects

  • Character-level formatting: Apply bold, italic, font size, and color to individual Portion objects.
  • Solid and gradient fills: Set shape fill to a solid color or multi-stop linear gradient.
  • Shadow and glow effects: Apply outer shadow, glow, blur, and reflection to any shape.
  • Paragraph alignment: Set left, center, right, or justify alignment per paragraph.
  • Round-trip safe: Unknown XML parts are preserved verbatim on re-save.

Developer Experience

Aspose.Slides FOSS installs with a single pip install aspose-slides-foss command. The only runtime dependency is lxml, installed automatically. There are no native extensions to compile.

The API mirrors PowerPoint’s own object model (Presentation, Slide, Shape, TextFrame, Paragraph, Portion), so anyone familiar with the PowerPoint object model can use the library immediately. It is MIT-licensed, open-source on GitHub, and requires Python 3.10 or later.

Create a Presentation and Add a Shape

Use the context manager (with slides.Presentation() as prs:) to ensure the PPTX is always closed and resources are freed. add_auto_shape() takes a ShapeType enum, then x/y position and width/height in points — the shape’s text_frame.text property sets the label in one line.

pip install aspose-slides-foss
import aspose.slides_foss as slides

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

    # Add a rectangle AutoShape
    shape = slide.shapes.add_auto_shape(
        slides.ShapeType.RECTANGLE, 50, 50, 400, 150
    )
    shape.text_frame.text = "Hello, Aspose.Slides!"

    prs.save("output.pptx", slides.export.SaveFormat.PPTX)

Format Text and Apply a Fill Effect

Text formatting works at the Portion level — the smallest unit of a run of characters. Open the saved file, navigate to the first portion of the first paragraph, and set font properties directly. Shape fill is independent: set fill_type to SOLID and assign a color to solid_fill_color.color.

import aspose.slides_foss as slides
from aspose.slides_foss import NullableBool
from aspose.slides_foss.drawing import Color

with slides.Presentation("output.pptx") as prs:
    shape = prs.slides[0].shapes[0]
    portion = shape.text_frame.paragraphs[0].portions[0]

    # Bold, 18pt, dark-blue text
    portion.portion_format.font_bold = NullableBool.TRUE
    portion.portion_format.font_height = 18
    portion.portion_format.fill_format.solid_fill_color.color = Color.dark_blue

    # Solid background fill on the shape
    shape.fill_format.fill_type = slides.FillType.SOLID
    shape.fill_format.solid_fill_color.color = Color.alice_blue

    prs.save("formatted.pptx", slides.export.SaveFormat.PPTX)

常见问题

What is Aspose.Slides FOSS for Python?

It is a free, MIT-licensed pure-Python library for creating, reading, and editing PowerPoint .pptx presentations without requiring Microsoft Office.

Which file formats are supported?

PPTX is the supported read/write format. Export to PDF, HTML, SVG, or images is not available in this edition.

Does it require Microsoft Office or PowerPoint?

No. Aspose.Slides FOSS is a pure-Python library with no dependency on Microsoft Office, COM automation, or any proprietary runtime.

How do I install it?

Run pip install aspose-slides-foss. The only dependency is lxml, installed automatically. Python 3.10 or later is required.

Can I apply visual effects like shadow and glow?

Yes. The library supports outer shadow, glow, blur, and reflection effects on any shape object.

Is the context manager pattern recommended?

Yes. Always open a Presentation with with slides.Presentation(...) as prs: to ensure reliable resource cleanup.

Will round-tripping a PPTX destroy unknown content?

No. Unknown XML parts encountered during load are preserved verbatim on save, so content the library does not yet understand is never lost.

Where can I find the source code?

The library is MIT-licensed and hosted on GitHub. Bug reports and pull requests are welcome.

  

支持和学习资源

 中文