Pythonでプレゼンテーションを読み込む方法

Pythonでプレゼンテーションを読み込む方法

Aspose.Slides FOSS for Python lets you open any .pptx ファイルを検査し、その内容を確認して、PPTX に再保存するかデータを抽出します。このガイドでは、ファイルの開き方、スライドの反復処理、シェイプのテキスト読み取り、そして保存のラウンドトリップについて説明します。.

ステップバイステップ ガイド

ステップ 1: パッケージをインストールする

pip install aspose-slides-foss

ステップ 2: 既存のプレゼンテーションを開く

ファイルパスを渡す slides.Presentation().コンテキストマネージャを使用してクリーンアップを確実に行います。.

import aspose.slides_foss as slides
from aspose.slides_foss.export import SaveFormat

with slides.Presentation("input.pptx") as prs:
    print(f"Slide count: {len(prs.slides)}")
    prs.save("output.pptx", SaveFormat.PPTX)

ソースファイル内の未知の XML パーツはそのまま保持されます:ライブラリはまだ理解できないコンテンツを削除することはありません。.


ステップ 3: スライドを検査する

すべてのスライドを反復し、インデックスを出力します:

import aspose.slides_foss as slides

with slides.Presentation("deck.pptx") as prs:
    for i, slide in enumerate(prs.slides):
        shape_count = len(slide.shapes)
        print(f"Slide {i}: {shape_count} shapes")

ステップ 4: シェイプテキストを読む

シェイプを反復処理し、テキストを持つシェイプからテキストを読み取ります。 TextFrame:

import aspose.slides_foss as slides

with slides.Presentation("deck.pptx") as prs:
    for slide in prs.slides:
        for shape in slide.shapes:
            if hasattr(shape, "text_frame") and shape.text_frame is not None:
                text = shape.text_frame.text
                if text.strip():
                    print(f"  Shape text: {text!r}")

ステップ 5: ドキュメントプロパティを読む

コアドキュメントプロパティにアクセスします prs.document_properties:

import aspose.slides_foss as slides

with slides.Presentation("deck.pptx") as prs:
    props = prs.document_properties
    print(f"Title:   {props.title}")
    print(f"Author:  {props.author}")
    print(f"Subject: {props.subject}")

ステップ 6: ラウンドトリップ保存

プレゼンテーションを検査または変更した後、PPTX に再保存します:

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

別のパスに保存すると新しいファイルが作成されます。同じパスに保存すると元のファイルが上書きされます。.


一般的な問題と対策

FileNotFoundError

パスが正しいことを確認します .pptx ファイルが作業ディレクトリに対して正しいことを確認してください。使用します pathlib.Path 堅牢なパス構築のために使用します::

from pathlib import Path
path = Path(__file__).parent / "assets" / "deck.pptx"
with slides.Presentation(str(path)) as prs:
    ...

Exception: File format is not supported

このライブラリはサポートしています .pptx (Office Open XML) のみです。レガシー .ppt (binary PowerPoint 97–2003) ファイルはサポートされていません。.

シェイプに text_frame 属性がありません

一部のシェイプ(Connectors、PictureFrames、GroupShapes)には a がありません text_frame.Guard with hasattr(shape, "text_frame") and shape.text_frame is not None テキストにアクセスする前に。.


よくある質問

読み込みは元のコンテンツをすべて保持しますか??

はい。未知の XML パーツはラウンドトリップ保存時にそのまま保持されます。ライブラリはまだ理解できない XML コンテンツを削除しません。.

パスワードで保護された PPTX を読み込めますか??

パスワード保護(暗号化)されたプレゼンテーションはこのエディションではサポートされていません。.

埋め込み画像を抽出できますか??

画像コレクションにアクセスする: prs.images 返します ImageCollection.。各画像には a が content_type と a bytes 生の画像データを読み取るためのプロパティです。.

インメモリストリームからのロードはサポートされていますか??

直接からロードする io.BytesIO は現在の API では公開されていません。まずバイトを一時ファイルに書き込んでください:

import tempfile, os
import aspose.slides_foss as slides

with tempfile.NamedTemporaryFile(suffix=".pptx", delete=False) as tmp:
    tmp.write(pptx_bytes)
    tmp_path = tmp.name

try:
    with slides.Presentation(tmp_path) as prs:
        print(f"Slides: {len(prs.slides)}")
finally:
    os.unlink(tmp_path)

参照

 日本語