はじめに

このガイドでは、PowerPoint プレゼンテーションを Python でプログラム的に作成、変更、保存する方法を Aspose.Slides FOSS を使用して示します。ライブラリを使えば、スライドをゼロから構築し、スライドの追加や順序変更、図形・表・書式付きテキストで内容を埋め、結果を PPTX として保存できます。すべての処理は純粋な Python で行われ、Microsoft Office や外部バイナリへの依存はありません。

このライブラリは PyPI 上で aspose-slides-foss として MIT ライセンスの下で提供されています。pip でインストールし、インポートして、数行のコードでプレゼンテーションの作成を開始できます。レポート生成の自動化、スライドデックパイプラインの構築、あるいはウェブアプリケーションへのプレゼンテーション出力の埋め込みなど、何を行う場合でも、API はスライドコレクション、マスター/レイアウトスライド、シェイプコレクション、テキストフレーム、塗りつぶし形式、エクスポートオプションといった全機能をカバーしています。

この投稿では、Python でスライドを扱う際に最も頻繁に使用するクラスやメソッドといった、コアとなるプレゼンテーション管理機能を解説します。


含まれるもの

プレゼンテーションのライフサイクル

Presentation クラスはエントリーポイントです。新しいデッキを作成し、内容を操作して PPTX として保存します。Presentation を with と組み合わせたコンテキストマネージャとして使用すると、ファイルハンドルが自動的に解放されます。

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)

スライド コレクション操作

SlideCollection を使用すると、スライドの追加、挿入、クローン、削除、および再配置ができます。len(pres.slides) は各変更後の現在のカウントを反映します。

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)

シェイプと AutoShapes

任意のスライドに対して、ShapeCollection.add_auto_shape() を介して長方形、楕円、三角形、その他の幾何学的シェイプを追加できます。各シェイプは位置、サイズ、回転、塗りつぶしプロパティを提供し、保存/再読み込みサイクルを通じて保持されます。

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

テーブル

ShapeCollection.add_table() でデータテーブルを作成し、TextFrame を使用してセルのテキストを設定し、セルを結合し、枠線の書式設定を適用します。行の高さと列の幅はコンストラクタに渡す値と一致します。

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])

    # 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

マスタースライドとレイアウトスライド

すべてのプレゼンテーションには、マスタースライドとレイアウトスライドの階層があります。Presentation.masters と Presentation.layout_slides を介してそれらにアクセスし、デッキ全体で一貫した書式設定を適用します。

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)

ドキュメントプロパティ

タイトル、著者、テーマなどの組み込みメタデータは、DocumentProperties オブジェクトを介して読み書きできます。save() を呼び出す前にこれらのプロパティを設定してください — これらは PPTX メタデータに埋め込まれ、下流ツールで読み取れます:

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)

クイックスタート

以下の pip コマンドを実行して、PyPI から Python 用の Aspose.Slides FOSS をインストールしてください:

pip install aspose-slides-foss

以下の例は、タイトルシェイプ、データテーブル、塗りつぶしを含むプレゼンテーションを作成し、PPTX として保存します:

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)

サポートされているフォーマット

フォーマット拡張子読み取り書き込み
PPTX.pptx✓✓
PPT.ppt—Planned
PPSX.ppsx—Planned
PPTM.pptm—Planned
POTX.potx—Planned
ODP.odp—Planned
PDF.pdf—Planned
HTML.html—Planned
HTML5.html—Planned
TIFF.tiff—Planned
GIF.gif—Planned
XPS.xps—Planned
FODP.fodp—Planned
MD.md—Planned

注: PPTX の読み取りと書き込みは完全に実装されています。その他 SaveFormat 値 (PPT, ODP, PDF, 等) は将来のリリースで予定されています; それらを save() 現在は PPTX 出力を生成します。


オープンソースとライセンス

Aspose.Slides FOSS for Python は MIT ライセンスの下でリリースされています。個人、学術、商用プロジェクトで制限なく使用できます。ソースコードは GitHub で入手でき、パッケージは PyPI に公開されています。


はじめに

関連リソース