Python에서 프레젠테이션 로드하는 방법
Aspose.Slides FOSS for Python은 모든 .pptx 파일을 열고, 내용을 검사하며, PPTX로 다시 저장하거나 데이터를 추출할 수 있게 해줍니다. 이 가이드는 파일 열기, 슬라이드 반복, 도형 텍스트 읽기, 저장 라운드 트립에 대해 다룹니다.
단계별 가이드
1단계: 패키지 설치
pip install aspose-slides-foss2단계: 기존 프레젠테이션 열기
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}")Step 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) 파일은 지원되지 않습니다.
Shapes에는 text_frame 속성이 없습니다.
일부 도형(Connectors, PictureFrames, GroupShapes)에는 text_frame이 없습니다. 텍스트에 접근하기 전에 hasattr(shape, "text_frame") and shape.text_frame is not None으로 보호하십시오.
자주 묻는 질문
로드가 모든 원본 콘텐츠를 보존합니까?
예. 알 수 없는 XML 파트는 라운드 트립 저장 시 그대로 보존됩니다. 라이브러리는 아직 이해하지 못하는 XML 콘텐츠를 제거하지 않습니다.
비밀번호로 보호된 PPTX를 로드할 수 있나요?
이 버전에서는 암호로 보호된(암호화된) 프레젠테이션을 지원하지 않습니다.
내장된 이미지를 추출할 수 있나요?
이미지 컬렉션에 액세스합니다: prs.images는 ImageCollection를 반환합니다. 각 이미지는 원시 이미지 데이터를 읽기 위한 content_type 및 bytes 속성을 가지고 있습니다.
인메모리 스트림에서 로드하는 것이 지원됩니까?
현재 API에서는 io.BytesIO에서 직접 로드하는 것이 노출되지 않습니다. 먼저 바이트를 임시 파일에 기록하십시오:
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)