使用 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 活頁簿。範例將一個 200 × 150 點的框架放置於第一張投影片的上緣與左緣各 20 點處。必須保持活頁簿在儲存的路徑可存取,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 儲存格,並以 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,請使用收件者可存取的完整路徑,或將檔案嵌入簡報中。