[python] python-pptx 이미지 효과 적용하기
python-pptx는 Python을 사용하여 PowerPoint 문서를 생성하고 편집할 수 있는 라이브러리입니다. 이 라이브러리를 사용하면 Python 코드에서 이미지 파일을 PowerPoint 슬라이드에 추가하고, 다양한 효과를 적용할 수 있습니다.
이번에는 python-pptx를 사용하여 이미지에 효과를 적용하는 방법에 대해 알아보겠습니다.
설치
먼저 python-pptx 라이브러리를 설치해야 합니다. 아래의 명령어를 사용하여 설치할 수 있습니다.
pip install python-pptx
이미지 추가하기
python-pptx를 사용하여 이미지를 추가하려면 먼저 Presentation 객체를 생성해야 합니다. 그리고 add_picture 메서드를 사용하여 이미지를 슬라이드에 추가할 수 있습니다.
from pptx import Presentation
# Presentation 객체 생성
presentation = Presentation()
# 슬라이드 생성
slide = presentation.slides.add_slide(presentation.slide_layouts[0])
# 이미지 추가
slide.shapes.add_picture('image.jpg', left=100, top=100, width=300, height=200)
# 파일 저장
presentation.save('presentation.pptx')
위의 예제에서는 ‘image.jpg’ 파일을 슬라이드에 추가하고, left, top, width, height 속성을 사용하여 이미지의 위치와 크기를 조정하고 있습니다.
이미지 효과 적용하기
python-pptx를 사용하여 이미지에 효과를 적용하려면 Picture 객체를 가져와서 해당 객체의 특성을 조정해야 합니다. 아래 예제에서는 이미지를 회전시키고 크기를 조정하는 방법을 보여줍니다.
from pptx import Presentation
from pptx.util import Inches
# Presentation 객체 생성
presentation = Presentation()
# 슬라이드 생성
slide = presentation.slides.add_slide(presentation.slide_layouts[0])
# 이미지 추가
picture = slide.shapes.add_picture('image.jpg', left=100, top=100, width=300, height=200)
# 이미지 효과 적용
picture.rotation = 45 # 이미지를 45도 회전
picture.width = Inches(4) # 이미지의 너비를 4인치로 조정
# 파일 저장
presentation.save('presentation.pptx')
위의 예제에서는 rotation
속성을 사용하여 이미지를 45도 회전시키고, width
속성을 사용하여 이미지의 너비를 4인치로 조정하고 있습니다.
다양한 이미지 효과를 적용하기 위해서는 python-pptx 라이브러리의 문서나 예제를 참고하는 것이 도움이 될 수 있습니다.