Python via Java에서 프레젠테이션 슬라이드의 도형 크기 조정
개요
Aspose.Slides for Python via Java 고객이 가장 흔히 묻는 질문 중 하나는 슬라이드 크기가 변경될 때 데이터가 잘리지 않도록 도형의 크기를 조정하는 방법이다. 이 짧은 기술 문서에서는 그 방법을 보여준다.
도형 크기 조정
슬라이드 크기가 변경될 때 도형이 잘못 정렬되는 것을 방지하려면 각 도형의 위치와 크기를 새로운 슬라이드 레이아웃에 맞게 업데이트하십시오.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat, SlideSizeType, SlideSizeScaleType, SlideOrientation
# 프레젠테이션 파일을 로드합니다.
presentation = Presentation("sample.ppt")
try:
# 원본 슬라이드 크기를 가져옵니다.
current_height = presentation.getSlideSize().getSize().getHeight()
current_width = presentation.getSlideSize().getSize().getWidth()
# 기존 도형을 스케일링하지 않고 슬라이드 크기를 변경합니다.
presentation.getSlideSize().setSize(SlideSizeType.A4Paper, SlideSizeScaleType.DoNotScale)
# 새로운 슬라이드 크기를 가져옵니다.
new_height = presentation.getSlideSize().getSize().getHeight()
new_width = presentation.getSlideSize().getSize().getWidth()
height_ratio = new_height / current_height
width_ratio = new_width / current_width
# 모든 슬라이드의 도형 크기를 조정하고 위치를 재설정합니다.
for slide in presentation.getSlides():
for shape in slide.getShapes():
# 도형 크기를 스케일링합니다.
shape.setHeight(shape.getHeight() * height_ratio)
shape.setWidth(shape.getWidth() * width_ratio)
# 도형 위치를 스케일링합니다.
shape.setY(shape.getY() * height_ratio)
shape.setX(shape.getX() * width_ratio)
presentation.save("output.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Note
표는 별도의 처리가 필요하지 않습니다: 표의 너비와 높이를 설정하면 열과 행이 비례적으로 재조정되므로 행 높이와 열 너비를 다시 스케일링하면 비율이 두 번 적용됩니다.import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat, SlideSizeType, SlideSizeScaleType, SlideOrientation
presentation = Presentation("sample.pptx")
try:
# 원본 슬라이드 크기를 가져옵니다.
current_height = presentation.getSlideSize().getSize().getHeight()
current_width = presentation.getSlideSize().getSize().getWidth()
# 기존 도형을 스케일링하지 않고 슬라이드 크기를 변경합니다.
presentation.getSlideSize().setSize(SlideSizeType.A4Paper, SlideSizeScaleType.DoNotScale)
# presentation.getSlideSize().setOrientation(SlideOrientation.Portrait)
# 새로운 슬라이드 크기를 가져옵니다.
new_height = presentation.getSlideSize().getSize().getHeight()
new_width = presentation.getSlideSize().getSize().getWidth()
height_ratio = new_height / current_height
width_ratio = new_width / current_width
for master in presentation.getMasters():
for shape in master.getShapes():
# 도형 크기를 스케일링합니다.
shape.setHeight(shape.getHeight() * height_ratio)
shape.setWidth(shape.getWidth() * width_ratio)
# 도형 위치를 스케일링합니다.
shape.setY(shape.getY() * height_ratio)
shape.setX(shape.getX() * width_ratio)
for layout_slide in master.getLayoutSlides():
for shape in layout_slide.getShapes():
# 도형 크기를 스케일링합니다.
shape.setHeight(shape.getHeight() * height_ratio)
shape.setWidth(shape.getWidth() * width_ratio)
# 도형 위치를 스케일링합니다.
shape.setY(shape.getY() * height_ratio)
shape.setX(shape.getX() * width_ratio)
for slide in presentation.getSlides():
for shape in slide.getShapes():
# 도형 크기를 스케일링합니다.
shape.setHeight(shape.getHeight() * height_ratio)
shape.setWidth(shape.getWidth() * width_ratio)
# 도형 위치를 스케일링합니다.
shape.setY(shape.getY() * height_ratio)
shape.setX(shape.getX() * width_ratio)
presentation.save("output.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
자주 묻는 질문
슬라이드 크기를 조정한 후 도형이 왜 왜곡되거나 잘리는가?
슬라이드 크기를 조정하면 도형은 명시적으로 스케일을 변경하지 않는 한 원래 위치와 크기를 유지합니다. 이로 인해 내용이 잘리거나 도형이 정렬이 어긋날 수 있습니다.
제공된 코드가 모든 도형 유형에 대해 작동합니까?
예. 높이와 너비를 설정하면 텍스트 상자, 이미지, 차트, 표 등 모든 도형에 동일하게 적용됩니다.
슬라이드 크기를 조정할 때 표의 크기를 어떻게 조정합니까?
다른 도형과 마찬가지로 표 도형 자체를 스케일링하면 됩니다. 행과 열이 비례적으로 따라오므로 이후에 다시 스케일링하지 마세요.
이 크기 조정이 마스터 슬라이드와 레이아웃 슬라이드에도 적용됩니까?
예, 전체 프레젠테이션이 새로운 슬라이드 크기를 따르도록 하려면 Presentation.getMasters와 Presentation.getLayoutSlides를 순회하면서 해당 도형에도 동일한 스케일링 로직을 적용해야 합니다.
크기 조정과 함께 슬라이드의 방향(세로/가로)을 변경할 수 있나요?
예. SlideSize.setOrientation를 사용하여 방향을 변경할 수 있습니다. 레이아웃을 유지하려면 스케일링 로직을 그에 맞게 설정하세요.
설정할 수 있는 슬라이드 크기에 제한이 있나요?
Aspose.Slides는 사용자 지정 크기를 지원하지만, 매우 큰 크기는 성능이나 일부 PowerPoint 버전과의 호환성에 영향을 줄 수 있습니다.
고정 종횡비 도형이 왜곡되는 것을 어떻게 방지할 수 있나요?
스케일링하기 전에 도형 잠금의 getAspectRatioLocked 메서드를 확인하세요. 종횡비가 잠겨 있으면 개별적으로 스케일링하지 말고 너비 또는 높이를 비례적으로 조정하십시오.