[python] python-pptx 자동화 스크립트 작성하기

이번에는 python-pptx 라이브러리를 사용하여 PowerPoint 프레젠테이션을 자동으로 생성하는 스크립트를 작성해보겠습니다.

패키지 설치하기

먼저 python-pptx 패키지를 설치해야 합니다. 아래의 명령을 사용하여 설치할 수 있습니다.

pip install python-pptx

스크립트 작성하기

# 필요한 모듈 임포트하기
from pptx import Presentation
from pptx.util import Inches

# 프레젠테이션 객체 생성하기
presentation = Presentation()

# 슬라이드 추가하기
slide1 = presentation.slides.add_slide(presentation.slide_layouts[0])
title = slide1.shapes.title
title.text = "첫 번째 슬라이드"

slide2 = presentation.slides.add_slide(presentation.slide_layouts[1])
title = slide2.shapes.title
title.text = "두 번째 슬라이드"

# 텍스트 상자 추가하기
slide = presentation.slides.add_slide(presentation.slide_layouts[1])
left = Inches(1)
top = Inches(1)
width = Inches(8)
height = Inches(2)
text_box = slide.shapes.add_textbox(left, top, width, height)
text_frame = text_box.text_frame
text_frame.text = "텍스트 상자입니다."

# 이미지 추가하기
slide = presentation.slides.add_slide(presentation.slide_layouts[1])
left = Inches(1)
top = Inches(2)
width = Inches(6)
height = Inches(4)
slide.shapes.add_picture("image.jpg", left, top, width, height)

# 프레젠테이션 저장하기
presentation.save("example.pptx")

위의 코드는 python-pptx를 사용하여 슬라이드를 추가하고 텍스트 상자와 이미지를 추가하는 방법을 보여줍니다. 프레젠테이션을 저장하고 메모리에서 해제하는 것을 잊지 마세요.

실행하기

위의 코드를 presentation_script.py와 같은 파일로 저장한 후 실행하면 example.pptx라는 이름의 프레젠테이션 파일이 생성됩니다.

자동화된 프레젠테이션 생성에는 다양한 기능과 옵션을 사용할 수 있으며, python-pptx의 문서를 참조하여 더 많은 정보를 얻을 수 있습니다.

참고 자료