[python] python-pptx 슬라이드 배경 설정하기

python-pptx는 파이썬을 통해 Powerpoint 파일을 생성하고 편집할 수 있는 라이브러리입니다. 이 라이브러리를 사용하여 슬라이드 배경을 설정하는 방법을 알아보겠습니다.

1. python-pptx 설치하기

먼저 python-pptx를 설치해야 합니다. 다음 명령을 사용하여 설치할 수 있습니다.

pip install python-pptx

2. 슬라이드 생성하기

슬라이드를 생성하려면 Presentation 객체를 만들어야 합니다. 다음 예제와 같이 슬라이드를 생성할 수 있습니다.

from pptx import Presentation

# Presentation 객체 생성
prs = Presentation()

# 슬라이드 추가
slide = prs.slides.add_slide(prs.slide_layouts[0])

3. 슬라이드 배경 설정하기

슬라이드 배경을 설정하려면 slide.background 속성을 사용해야 합니다. 다음 예제와 같이 배경 이미지를 설정할 수 있습니다.

from pptx.dml.color import RGBColor
from pptx.util import Inches

# 배경 색상 설정
slide.background.fill.solid()
slide.background.fill.fore_color.rgb = RGBColor(255, 255, 255)  # 흰색 배경

# 배경 이미지 설정
slide.background.fill.background()
slide.background.fill.background_style = 1  # 이미지 타일링
slide.background.fill.fore_color.rgb = RGBColor(0, 0, 0)  # 배경 이미지가 없는 경우에 대한 색상 설정

# 배경 이미지 크기 조정
slide.background.fill.picture.crop_left = 0
slide.background.fill.picture.crop_right = 0
slide.background.fill.picture.crop_top = 0
slide.background.fill.picture.crop_bottom = 0
slide.background.fill.picture_left = Inches(0)
slide.background.fill.picture_top = Inches(0)
slide.background.fill.picture_width = prs.slide_width
slide.background.fill.picture_height = prs.slide_height

4. 완성된 슬라이드 저장하기

모든 설정을 마치고 슬라이드를 저장하려면 Presentation 객체의 save 메서드를 사용합니다. 다음 예제와 같이 파일 이름을 지정하여 슬라이드를 저장할 수 있습니다.

prs.save("slide.pptx")

위의 예제를 참고하여 python-pptx를 사용하여 슬라이드 배경을 설정해 보세요. 자세한 내용은 python-pptx 공식 문서를 참조하시기 바랍니다.

참조: