[python] python-pptx 슬라이드 썸네일 생성하기

python-pptx는 파이썬에서 PowerPoint 파일을 조작할 수 있게 해주는 라이브러리입니다. 이 라이브러리를 사용하면 PowerPoint에 슬라이드를 추가하고, 텍스트를 삽입하고, 슬라이드를 삭제하는 등의 작업을 할 수 있습니다.

이번에는 python-pptx를 사용하여 슬라이드 썸네일을 생성하는 방법에 대해 알아보겠습니다.

필요한 라이브러리 설치하기

먼저 python-pptx 라이브러리를 설치해야 합니다. 아래 명령어를 사용하여 설치할 수 있습니다.

pip install python-pptx

슬라이드 썸네일 생성하기

슬라이드 썸네일을 생성하기 위해서는 Presentation 객체와 Slide 객체를 사용해야 합니다. 먼저 PowerPoint 파일을 열고 Presentation 객체를 생성합니다.

from pptx import Presentation

# PowerPoint 파일 열기
presentation = Presentation('example.pptx')

다음으로 Presentation.slides 속성을 사용하여 각 슬라이드에 접근할 수 있습니다. 각 슬라이드에는 썸네일 이미지를 생성하는 Slide.export 메서드를 사용할 수 있습니다.

# 각 슬라이드의 썸네일 생성
for i, slide in enumerate(presentation.slides):
    thumbnail = slide.export(f'thumbnail_{i}.png')
    thumbnail.save()

위 코드에서는 enumerate 함수를 사용하여 슬라이드의 인덱스와 해당 슬라이드를 가져오고, Slide.export 메서드를 사용하여 썸네일 이미지를 생성합니다. 이렇게 생성된 이미지를 저장하면 됩니다.

완성된 코드

아래는 전체적인 코드의 예시입니다.

from pptx import Presentation

# PowerPoint 파일 열기
presentation = Presentation('example.pptx')

# 각 슬라이드의 썸네일 생성
for i, slide in enumerate(presentation.slides):
    thumbnail = slide.export(f'thumbnail_{i}.png')
    thumbnail.save()

위 코드를 실행하면 example.pptx 파일에서 각 슬라이드의 썸네일 이미지가 생성되고, 현재 디렉토리에 thumbnail_0.png, thumbnail_1.png와 같은 이름으로 저장됩니다.

결론

python-pptx를 사용하면 파이썬으로 PowerPoint 파일을 조작할 수 있습니다. 슬라이드 썸네일을 생성하는 방법을 알아보았는데, 이를 활용하여 다양한 PowerPoint 자동화 작업을 할 수 있습니다.

더 자세한 정보는 python-pptx 공식 문서를 참고하시기 바랍니다.