Python を使用してプレゼンテーションの OLE オブジェクトを管理
導入
Object Linking and Embedding (OLE) を使用すると、PowerPoint のスライドに別のアプリケーションで作成されたオブジェクト(たとえば Excel ブックからのチャート)を含めることができます。埋め込みオブジェクトはデータをプレゼンテーションに保存します。リンクされたオブジェクトは外部ファイルを参照します。
PowerPoint はオブジェクトをアイコンとして表示したり、内容のプレビューを表示したりできます。オブジェクトをダブルクリックすると、関連アプリケーションが利用可能な場合に編集用にアクティブ化されます。Aspose.Slides for Python via .NET は埋め込みおよびリンクされたオブジェクトの両方を OleObjectFrame として表します。
スライドへの OLE オブジェクト フレームの追加
チャートを含む Excel ワークブックを埋め込むには、ワークブックのバイト列と xlsx 拡張子を OleEmbeddedDataInfo に渡します。拡張子はファイルタイプを識別し、PowerPoint が対応するアプリケーションを選択できるようにします。この例では、add_ole_object_frame がワークブックを最初のスライドに配置し、フレームのサイズをスライド全体に合わせます。
from pathlib import Path
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
slide_size = presentation.slide_size.size
file_bytes = Path("book.xlsx").read_bytes()
data = slides.dom.ole.OleEmbeddedDataInfo(file_bytes, "xlsx")
slide.shapes.add_ole_object_frame(0, 0, slide_size.width, slide_size.height, data)
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
リンクされた OLE オブジェクト フレームの追加
add_ole_object_frame のリンク版は OLE クラス識別子とソースファイルへのパスを受け取ります。ここでは Excel.Sheet.12 が Excel ワークブックを識別します。この例は、最初のスライドの上端と左端からそれぞれ 20 ポイント離れた位置に、200 × 150 ポイントのフレームを配置します。PowerPoint が開くためには、ワークブックが保存されたパスに残っている必要があります。
from pathlib import Path
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
workbook_path = str(Path("book.xlsx").resolve())
slide.shapes.add_ole_object_frame(20, 20, 200, 150, "Excel.Sheet.12", workbook_path)
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
OLE オブジェクト フレームへのアクセス
embedded_data を使用して、埋め込み OLE フレームに保存されたファイルを読み取ります。この例は最初のスライドの最初のシェイプを調べ、埋め込みデータが利用可能な場合にファイル拡張子とバイト数を出力します。
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
shapes = presentation.slides[0].shapes
frame = shapes[0] if len(shapes) else None
if not isinstance(frame, slides.OleObjectFrame) or frame.is_object_link:
print("The first shape is not an embedded OLE object.")
else:
data = frame.embedded_data
file_bytes = data.embedded_file_data if data is not None else None
if not file_bytes:
print("The frame contains no embedded file data.")
else:
print(f"Embedded file extension: {data.embedded_file_extension}, size: {len(file_bytes)} bytes.")
リンクされた OLE オブジェクト フレーム プロパティへのアクセス
リンクされたフレームの場合、is_object_link は True で、link_path_long がソースファイルを示します。この例は最初のスライドの最初のシェイプの保存されたパスを出力します。PPT プレゼンテーションはまた、link_path_relative に相対パスを格納でき、利用可能な場合はそれも出力します。
import aspose.slides as slides
with slides.Presentation("sample.ppt") as presentation:
shapes = presentation.slides[0].shapes
frame = shapes[0] if len(shapes) else None
if not isinstance(frame, slides.OleObjectFrame) or not frame.is_object_link:
print("The first shape is not a linked OLE object.")
else:
print(f"Linked file: {frame.link_path_long}")
if frame.link_path_relative:
print(f"Relative path: {frame.link_path_relative}")
OLE オブジェクト データの変更
Note
この例では、埋め込みワークブックを編集するために Aspose.Cells for Python via .NET を使用します。この例は最初のスライドの最初のシェイプに埋め込まれた XLSX ワークブックがあることを前提としています。メモリ上でワークブックを開き、最初のワークシートのセル E1:E4 にラベルと 3 つの値を書き込み、XLSX 形式でメモリに保存し直します。set_embedded_data はフレームに保存されているデータを置き換え、編集されたセルが埋め込みワークブックを開く際に利用できるようにします。
from io import BytesIO
import aspose.cells as cells
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
shapes = presentation.slides[0].shapes
frame = shapes[0] if len(shapes) else None
if not isinstance(frame, slides.OleObjectFrame) or frame.is_object_link:
print("The first shape is not an embedded OLE object.")
else:
data = frame.embedded_data
file_bytes = data.embedded_file_data if data is not None else None
if not file_bytes:
print("The frame contains no embedded file data.")
elif (data.embedded_file_extension or "").lstrip(".").lower() != "xlsx":
print("The embedded file is not an XLSX workbook.")
else:
workbook = cells.Workbook(BytesIO(file_bytes))
worksheet_cells = workbook.worksheets[0].cells
worksheet_cells.get("E1").put_value("E")
worksheet_cells.get("E2").put_value(12)
worksheet_cells.get("E3").put_value(14)
worksheet_cells.get("E4").put_value(15)
output = BytesIO()
workbook.save(output, cells.SaveFormat.XLSX)
updated_bytes = output.getvalue()
updated_data = slides.dom.ole.OleEmbeddedDataInfo(updated_bytes, "xlsx")
frame.set_embedded_data(updated_data)
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
スライドへの他のファイルタイプの埋め込み
HTML ドキュメント、PDF、ZIP アーカイブも埋め込むことができます。この例は add_ole_object_frame を使用して、HTML 文書と ZIP アーカイブを最初のスライドの別々のフレームに配置します。is_object_icon を True に設定すると、各ファイルがアイコンとして表示され、読者はダブルクリックで関連アプリケーションで開くことができます。
from pathlib import Path
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
html_bytes = Path("sample.html").read_bytes()
html_data = slides.dom.ole.OleEmbeddedDataInfo(html_bytes, "html")
html_frame = slide.shapes.add_ole_object_frame(150, 120, 50, 50, html_data)
html_frame.is_object_icon = True
zip_bytes = Path("sample.zip").read_bytes()
zip_data = slides.dom.ole.OleEmbeddedDataInfo(zip_bytes, "zip")
zip_frame = slide.shapes.add_ole_object_frame(150, 220, 50, 50, zip_data)
zip_frame.is_object_icon = True
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
埋め込みオブジェクトのファイルタイプの設定
OleEmbeddedDataInfo の拡張子は提供されたバイト列の形式を示します。最初のスライドの最初のシェイプに既に拡張子が誤っている ZIP アーカイブが含まれている場合は、set_embedded_data を使用してタイプを修正します。この例は現在の拡張子を出力し、同じアーカイブバイト列を zip 拡張子で保存します。
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
shapes = presentation.slides[0].shapes
frame = shapes[0] if len(shapes) else None
if not isinstance(frame, slides.OleObjectFrame) or frame.is_object_link:
print("The first shape is not an embedded OLE object.")
else:
data = frame.embedded_data
file_bytes = data.embedded_file_data if data is not None else None
if not file_bytes:
print("The frame contains no embedded file data.")
else:
print(f"Current embedded file extension: {data.embedded_file_extension}")
archive_data = slides.dom.ole.OleEmbeddedDataInfo(file_bytes, "zip")
frame.set_embedded_data(archive_data)
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
埋め込みオブジェクトのアイコン画像とタイトルの設定
埋め込みオブジェクトのアイコンをカスタマイズするには、プレゼンテーションの画像コレクションから画像を取得して substitute_picture_format に割り当て、substitute_picture_title でキャプションを設定します。この例は最初のスライドの最初のシェイプに PNG 画像を使用し、is_object_icon を有効にしてアイコンとキャプションを表示します。
from pathlib import Path
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
shapes = presentation.slides[0].shapes
frame = shapes[0] if len(shapes) else None
if not isinstance(frame, slides.OleObjectFrame) or frame.is_object_link:
print("The first shape is not an embedded OLE object.")
else:
image_bytes = Path("image.png").read_bytes()
icon_image = presentation.images.add_image(image_bytes)
frame.is_object_icon = True
frame.substitute_picture_format.picture.image = icon_image
frame.substitute_picture_title = "Embedded workbook"
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
OLE オブジェクト フレームのサイズ変更と位置変更の防止
PowerPoint がリンクオブジェクトのプレビューを更新すると、フレームが移動したりサイズが変わったりすることがあります。update_automatic を False に設定すると、そのリンクに対して手動更新が要求されます。この設定はリンクされた OLE フレームである最初のスライドの最初のシェイプに適用されます。
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
shapes = presentation.slides[0].shapes
frame = shapes[0] if len(shapes) else None
if not isinstance(frame, slides.OleObjectFrame) or not frame.is_object_link:
print("The first shape is not a linked OLE object.")
else:
frame.update_automatic = False
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
埋め込みファイルの抽出
embedded_file_data を読み取って元のバイト列を抽出し、embedded_file_extension で出力ファイル名の拡張子を決定します。この例は最初のスライドのトップレベルシェイプを走査し、リンクされたオブジェクトはスキップして、各埋め込みファイルを現在のディレクトリに書き出します。シェイプインデックスは同じ拡張子を持つファイルを区別します。
from pathlib import Path
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
shapes = presentation.slides[0].shapes
extracted = 0
for index, frame in enumerate(shapes):
if not isinstance(frame, slides.OleObjectFrame) or frame.is_object_link:
continue
data = frame.embedded_data
file_bytes = data.embedded_file_data if data is not None else None
if not file_bytes:
print(f"Shape {index} contains no embedded file data.")
continue
extension = (data.embedded_file_extension or "").lstrip(".")
filename = f"OLE_object_{index}.{extension}" if extension else f"OLE_object_{index}"
Path(filename).write_bytes(file_bytes)
extracted += 1
print(f"Extracted {extracted} embedded files.")
埋め込みおよびリンクされた OLE オブジェクトの一覧
object_prog_id プロパティは、アプリケーションに関連付けられた OLE オブジェクトのタイプを示します。たとえば Word.Document.12 は Word 文書を識別します。埋め込みファイルの拡張子は別の値です。ProgID 単体では保存されたデータの形式や有効性は確認できません。
この例は sample.pptx のすべてのスライドを走査し、GroupShape.shapes をたどって入れ子になったグループを調べます。各 OleObjectFrame について、スライド番号、shape name、is_object_link、元の ProgID を出力します。未知の ProgID はそのまま表示され、空の ProgID は最終列が空白のままになります。この例はメタデータのみを読み取り、OLE コンテンツの抽出、開封、またはアクティベートは行いません。
import aspose.slides as slides
def report_objects(shapes, slide_number):
for shape in shapes:
if isinstance(shape, slides.OleObjectFrame):
print(f"{slide_number} | {shape.name} | {shape.is_object_link} | {shape.object_prog_id or ''}")
elif isinstance(shape, slides.GroupShape):
report_objects(shape.shapes, slide_number)
with slides.Presentation("sample.pptx") as presentation:
print("Slide | Shape | is_object_link | object_prog_id")
for slide in presentation.slides:
report_objects(slide.shapes, slide.slide_number)
よくある質問
OLE オブジェクトを PDF または画像にエクスポートしたときに何が表示されますか?
エクスポート時には、スライド上に表示されているアイコンまたはプレビュー画像が出力されます。外観を制御するには、エクスポート前にカスタムプレビューを設定してください。埋め込みファイルを PDF 添付として含めるには、PdfOptions.include_ole_data を有効にします。このオプションはデフォルトで無効です。Preserve Embedded OLE Files as PDF Attachments を参照してください。
PowerPoint で OLE フレームを誤って変更しないようにするにはどうすればよいですか?
shape-level locks を適用して、フレームの移動やサイズ変更などの操作を制限します。
リンクされた Excel オブジェクトがプレゼンテーションを開くと移動したりサイズが変わったりするのはなぜですか?
PowerPoint はリンクされたワークシート範囲のサイズでプレビューを更新することがあります。サイズを一定に保つには、フレームを範囲に合わせるか、範囲を固定フレームにスケーリングし、対応するプレビューを提供してください。Working Solution for Worksheet Resizing を参照してください。
PPTX プレゼンテーションはリンクされた OLE オブジェクトの相対パスを保存できますか?
相対パスは古い PPT 形式でのみ使用可能です。PPTX では、受信者がアクセスできるフルパスを使用するか、ファイルをプレゼンテーションに埋め込んでください。