Python을 사용하여 프레젠테이션에서 OLE 관리
소개
Note
OLE (Object Linking & Embedding) 은 Microsoft 기술로, 한 애플리케이션에서 만든 데이터와 개체를 다른 애플리케이션에 연결하거나 삽입할 수 있게 합니다.예를 들어, Microsoft Excel에서 만든 차트를 PowerPoint 슬라이드에 배치하면 OLE 개체가 됩니다.
- OLE 개체는 아이콘으로 표시될 수 있습니다. 아이콘을 더블 클릭하면 해당 애플리케이션(예: Excel)에서 개체가 열리거나, 열거나 편집할 애플리케이션을 선택하라는 메시지가 표시됩니다.
- OLE 개체가 내용(예: 차트)을 표시할 수도 있습니다. 이 경우 PowerPoint가 삽입된 개체를 활성화하고 차트 인터페이스를 로드하여 PowerPoint 내에서 차트 데이터를 편집할 수 있게 합니다.
Aspose.Slides for Python은 OLE 개체를 OLE 개체 프레임(OleObjectFrame)으로 슬라이드에 삽입할 수 있습니다.
슬라이드에 OLE 개체 추가
이미 Microsoft Excel에서 차트를 만들고 이를 Aspose.Slides for Python을 사용해 OLE 개체 프레임으로 슬라이드에 삽입하려면 다음 단계를 수행하십시오:
- Presentation 클래스의 인스턴스를 생성합니다.
- 인덱스로 슬라이드에 대한 참조를 얻습니다.
- Excel 파일을 바이트 배열로 읽습니다.
- 슬라이드에 OleObjectFrame을 추가하고 바이트 배열 및 기타 OLE 개체 세부 정보를 제공합니다.
- 수정된 프레젠테이션을 PPTX 파일로 저장합니다.
아래 예제에서는 Excel 파일의 차트를 OleObjectFrame으로 슬라이드에 삽입합니다.
Note: OleEmbeddedDataInfo 생성자는 삽입 가능한 개체의 파일 확장자를 두 번째 매개변수로 받습니다. PowerPoint는 이 확장자를 사용해 파일 유형을 식별하고 OLE 개체를 열 적절한 애플리케이션을 선택합니다.
import aspose.slides as slides
with slides.Presentation() as presentation:
slide_size = presentation.slide_size.size
slide = presentation.slides[0]
# OLE 개체를 위한 데이터를 준비합니다.
with open("book.xlsx", "rb") as file_stream:
file_data = file_stream.read()
data_info = slides.dom.ole.OleEmbeddedDataInfo(file_data, "xlsx")
# 슬라이드에 OLE 개체 프레임을 추가합니다.
ole_frame = slide.shapes.add_ole_object_frame(0, 0, slide_size.width, slide_size.height, data_info)
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
연결된 OLE 개체 추가
Aspose.Slides for Python은 데이터를 삽입하는 대신 파일에 연결되는 OleObjectFrame을 추가할 수 있습니다.
다음 Python 예제는 슬라이드에 Excel 파일에 연결된 OleObjectFrame을 추가하는 방법을 보여줍니다:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
# 연결된 Excel 파일로 OLE 개체 프레임을 추가합니다.
slide.shapes.add_ole_object_frame(20, 20, 200, 150, "Excel.Sheet.12", "book.xlsx")
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
OLE 개체 액세스
슬라이드에 OLE 개체가 이미 삽입되어 있는 경우 다음과 같이 액세스할 수 있습니다:
- Presentation 클래스의 인스턴스를 생성하여 OLE 개체가 포함된 프레젠테이션을 로드합니다.
- 인덱스로 슬라이드에 대한 참조를 얻습니다.
- OleObjectFrame 모양에 접근합니다.
- OLE 개체 프레임을 얻은 후 필요한 작업을 수행합니다.
아래 예제는 OLE 개체 프레임(삽입된 Excel 차트)에 접근하여 파일 데이터를 가져옵니다. 이 예제에서는 첫 번째 슬라이드에 단일 모양이 있는 PPTX를 사용합니다.
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
shape = slide.shapes[0]
if isinstance(shape, slides.OleObjectFrame):
ole_frame = shape
# 삽입된 파일 데이터를 가져옵니다.
# 삽입된 파일의 확장자를 가져옵니다.
# ...
연결된 OLE 개체 속성 액세스
Aspose.Slides는 연결된 OLE 개체 프레임의 속성에 접근할 수 있게 합니다.
아래 Python 예제는 OLE 개체가 연결되어 있는지 확인하고, 연결된 경우 파일 경로를 가져옵니다:
import aspose.slides as slides
with slides.Presentation("sample.ppt") as presentation:
slide = presentation.slides[0]
shape = slide.shapes[0]
if isinstance(shape, slides.OleObjectFrame):
ole_frame = shape
# OLE 개체가 연결되어 있는지 확인합니다.
if ole_frame.is_object_link:
# 연결된 파일의 전체 경로를 출력합니다.
print("OLE object frame is linked to:", ole_frame.link_path_long)
# 존재한다면 연결된 파일의 상대 경로를 출력합니다.
# 상대 경로는 .ppt 프레젠테이션에만 포함될 수 있습니다.
if ole_frame.link_path_relative:
print("OLE object frame relative path:", ole_frame.link_path_relative)
OLE 개체 데이터 변경
Note
이 섹션에서는 아래 코드 예제가 Aspose.Cells for Python via .NET을 사용합니다.이미 슬라이드에 OLE 개체가 삽입되어 있는 경우 아래와 같이 개체에 접근하고 데이터를 수정할 수 있습니다:
- Presentation 클래스의 인스턴스를 생성하여 프레젠테이션을 로드합니다.
- 인덱스로 대상 슬라이드를 가져옵니다.
- OleObjectFrame 모양에 접근합니다.
- OLE 개체 프레임을 얻은 후 필요한 작업을 수행합니다.
Workbook객체를 생성하고 OLE 데이터를 읽습니다.- 원하는
Worksheet을 열어 데이터를 편집합니다. - 업데이트된
Workbook을 스트림에 저장합니다. - 해당 스트림을 사용해 OLE 개체의 데이터를 교체합니다.
아래 예제에서는 OLE 개체 프레임(삽입된 Excel 차트)에 접근하여 파일 데이터를 수정해 차트를 업데이트합니다. 이 샘플은 첫 번째 슬라이드에 단일 모양이 있는 기존 PPTX를 사용합니다.
import io
import aspose.slides as slides
import aspose.cells as cells
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
shape = slide.shapes[0]
if isinstance(shape, slides.OleObjectFrame):
ole_frame = shape
with io.BytesIO(ole_frame.embedded_data.embedded_file_data) as ole_stream:
# OLE 개체 데이터를 Workbook 객체로 읽습니다.
workbook = cells.Workbook(ole_stream)
with io.BytesIO() as new_ole_stream:
# 워크북 데이터를 수정합니다.
workbook.worksheets.get(0).cells.get(0, 4).put_value("E")
workbook.worksheets.get(0).cells.get(1, 4).put_value(12)
workbook.worksheets.get(0).cells.get(2, 4).put_value(14)
workbook.worksheets.get(0).cells.get(3, 4).put_value(15)
file_options = cells.OoxmlSaveOptions(cells.SaveFormat.XLSX)
workbook.save(new_ole_stream, file_options)
# OLE 프레임 개체 데이터를 변경합니다.
new_data = slides.dom.ole.OleEmbeddedDataInfo(new_ole_stream.getvalue(), ole_frame.embedded_data.embedded_file_extension)
ole_frame.set_embedded_data(new_data)
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
슬라이드에 파일 삽입
Excel 차트 외에도 Aspose.Slides for Python을 사용하면 슬라이드에 다른 파일 유형을 삽입할 수 있습니다. 예를 들어 HTML, PDF, ZIP 파일을 개체로 삽입할 수 있습니다. 사용자가 삽입된 개체를 더블 클릭하면 연결된 애플리케이션에서 자동으로 열리거나, 적절한 프로그램을 선택하라는 메시지가 표시됩니다.
다음 Python 코드는 HTML 및 ZIP 파일을 슬라이드에 삽입하는 방법을 보여줍니다:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
with open("sample.html", "rb") as html_stream:
html_data = html_stream.read()
html_data_info = slides.dom.ole.OleEmbeddedDataInfo(html_data, "html")
html_ole_frame = slide.shapes.add_ole_object_frame(150, 120, 50, 50, html_data_info)
html_ole_frame.is_object_icon = True
with open("sample.zip", "rb") as zip_stream:
zip_data = zip_stream.read()
zip_data_info = slides.dom.ole.OleEmbeddedDataInfo(zip_data, "zip")
zip_ole_frame = slide.shapes.add_ole_object_frame(150, 220, 50, 50, zip_data_info)
zip_ole_frame.is_object_icon = True
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
삽입된 개체의 파일 유형 설정
프레젠테이션 작업 시 기존 OLE 개체를 새 개체로 교체하거나 지원되지 않는 OLE 개체를 지원되는 개체로 교체해야 할 수 있습니다. Aspose.Slides for Python은 삽입된 개체의 파일 유형을 설정할 수 있게 하여 OLE 프레임 데이터나 파일 확장자를 업데이트할 수 있습니다.
다음 Python 코드는 삽입된 OLE 개체의 파일 유형을 zip으로 설정하는 방법을 보여줍니다:
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
ole_frame = slide.shapes[0]
file_extension = ole_frame.embedded_data.embedded_file_extension
file_data = ole_frame.embedded_data.embedded_file_data
print(f"Current embedded file extension is: {file_extension}")
# 파일 유형을 ZIP으로 변경합니다.
ole_frame.set_embedded_data(slides.dom.ole.OleEmbeddedDataInfo(file_data, "zip"))
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
삽입된 개체의 아이콘 이미지 및 제목 설정
OLE 개체를 삽입하면 아이콘 기반 미리보기가 자동으로 추가됩니다. 이 미리보기는 사용자가 OLE 개체에 접근하거나 열기 전에 보는 것입니다. 특정 이미지와 텍스트를 미리보기로 사용하려면 Aspose.Slides for Python을 사용해 아이콘 이미지와 제목을 설정할 수 있습니다.
다음 Python 코드는 삽입된 개체의 아이콘 이미지와 제목을 설정하는 방법을 보여줍니다:
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
ole_frame = slide.shapes[0]
# 프레젠테이션 리소스에 이미지를 추가합니다.
with slides.Images.from_file("image.png") as image:
ole_image = presentation.images.add_image(image)
# OLE 미리보기를 위한 제목과 이미지를 설정합니다.
ole_frame.substitute_picture_title = "My title"
ole_frame.substitute_picture_format.picture.image = ole_image
ole_frame.is_object_icon = True
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
OLE 개체 프레임이 크기 조정 및 위치 변경되는 것을 방지
연결된 OLE 개체를 슬라이드에 추가하면 프레젠테이션을 열 때 PowerPoint가 링크 업데이트를 요청할 수 있습니다. ‘링크 업데이트’를 선택하면 PowerPoint가 연결된 개체의 데이터로 미리보기를 새로 고쳐 OLE 개체 프레임의 크기와 위치가 변경될 수 있습니다. 개체 데이터 업데이트 프롬프트를 방지하려면 OleObjectFrame 클래스의 update_automatic 속성을 False로 설정하십시오:
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
ole_frame = slide.shapes[0]
ole_frame.update_automatic = False
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
삽입된 파일 추출
Aspose.Slides for Python은 슬라이드에 OLE 개체로 삽입된 파일을 다음과 같이 추출할 수 있습니다:
- 추출하려는 OLE 개체가 포함된 Presentation 클래스의 인스턴스를 생성합니다.
- 프레젠테이션의 모든 모양을 순회하여 OLEObjectFrame 모양을 찾습니다.
- 각 OLEObjectFrame에서 삽입된 파일 데이터를 가져와 디스크에 저장합니다.
다음 Python 코드는 슬라이드에 삽입된 파일을 OLE 개체로 추출하는 방법을 보여줍니다:
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
for index, shape in enumerate(slide.shapes):
if isinstance(shape, slides.OleObjectFrame):
ole_frame = shape
file_data = ole_frame.embedded_data.embedded_file_data
file_extension = ole_frame.embedded_data.embedded_file_extension
file_path = f"OLE_object_{index}{file_extension}"
with open(file_path, 'wb') as file_stream:
file_stream.write(file_data)
FAQ
슬라이드를 PDF/이미지로 내보낼 때 OLE 콘텐츠가 렌더링됩니까?
슬라이드에 표시되는 내용만 렌더링됩니다—아이콘/대체 이미지(미리보기)입니다. “실시간” OLE 콘텐츠는 렌더링 중에 실행되지 않습니다. 필요한 경우 자체 미리보기 이미지를 설정하여 내보낸 PDF에서 기대한 모양을 보장하십시오.
또한 삽입된 파일을 PDF 첨부 파일로 보존하려면 PdfOptions.include_ole_data 를 True 로 설정하십시오. 이 옵션은 기본적으로 비활성화되어 있습니다. 예제와 첨부 파일 확인 방법은 PDF 첨부 파일로 삽입된 OLE 파일 보존 을 참고하십시오.
PowerPoint에서 사용자가 슬라이드의 OLE 개체를 이동하거나 편집하지 못하도록 잠그려면 어떻게 해야 합니까?
모양을 잠그세요: Aspose.Slides는 모양 수준 잠금 을 제공합니다. 이는 암호화가 아니며, 실수로 인한 편집 및 이동을 효과적으로 방지합니다.
프레젠테이션을 열 때 연결된 Excel 개체가 “점프"하거나 크기가 변하는 이유는 무엇입니까?
PowerPoint가 연결된 OLE의 미리보기를 새로 고칠 수 있습니다. 안정적인 표시를 위해서는 워크시트 크기 조정에 대한 해결 방법 가이드에 따라 프레임을 범위에 맞추거나, 범위를 고정 프레임에 맞게 스케일링하고 적절한 대체 이미지를 설정하십시오.
PPTX 형식에서 연결된 OLE 개체의 상대 경로가 보존됩니까?
PPTX에서는 “상대 경로” 정보가 제공되지 않으며 전체 경로만 저장됩니다. 상대 경로는 이전 PPT 형식에서만 사용할 수 있습니다. 이동성을 위해서는 신뢰할 수 있는 절대 경로나 접근 가능한 URI, 혹은 삽입을 선호하십시오.