Python에서 PowerPoint 텍스트 문단 관리
개요
Aspose.Slides for Python via .NET 은 텍스트를 텍스트 프레임, 문단, Portion의 계층 구조로 표현합니다.
- TextFrame 텍스트 프레임은 도형 내에서 텍스트 컨테이너 역할을 하며 문단 컬렉션에 접근할 수 있게 합니다.
- Paragraph 문단은 텍스트 프레임 내의 하나의 문단을 나타내며 Portion 및 문단 수준 서식에 접근할 수 있습니다.
- Portion Portion은 문단 내의 텍스트 실행을 나타냅니다. 각 Portion은 자체 텍스트와 문자 수준 서식을 가질 수 있습니다.
따라서 문단은 여러 Portion을 사용하여 서로 다른 글꼴, 색상, 크기 및 기타 서식을 포함할 수 있습니다.
문단 만들기 및 서식 지정
여러 Portion을 사용하여 문단 만들기
다음 단계는 세 개의 문단을 가진 텍스트 프레임을 만들고, 각 문단에 세 개의 Portion을 포함합니다.
- Presentation 클래스의 인스턴스를 생성합니다.
- 인덱스로 해당 슬라이드에 접근합니다.
- 슬라이드에 사각형 AutoShape 을 추가합니다.
- 도형의 TextFrame 에 접근합니다.
- 기본 문단을 사용하고 두 개의 추가 Paragraph 객체를 텍스트 프레임에 추가합니다.
- 각 문단에 세 개의 Portion 객체를 추가합니다. 기본 문단에는 이미 빈 Portion 하나가 포함되어 있습니다.
- 각 Portion의 텍스트를 설정합니다.
- Portion.portion_format 을 통해 문자 수준 서식을 적용합니다.
- 수정된 프레젠테이션을 저장합니다.
이 Python 예제는 위 단계를 구현합니다:
import aspose.pydrawing as draw
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 50, 150, 300, 150)
text_frame = shape.text_frame
first_paragraph = text_frame.paragraphs[0]
first_paragraph.portions.add(slides.Portion())
first_paragraph.portions.add(slides.Portion())
second_paragraph = slides.Paragraph()
second_paragraph.portions.add(slides.Portion())
second_paragraph.portions.add(slides.Portion())
second_paragraph.portions.add(slides.Portion())
text_frame.paragraphs.add(second_paragraph)
third_paragraph = slides.Paragraph()
third_paragraph.portions.add(slides.Portion())
third_paragraph.portions.add(slides.Portion())
third_paragraph.portions.add(slides.Portion())
text_frame.paragraphs.add(third_paragraph)
for paragraph_index in range(text_frame.paragraphs.count):
paragraph = text_frame.paragraphs[paragraph_index]
for portion_index in range(paragraph.portions.count):
portion = paragraph.portions[portion_index]
portion.text = f"Portion {paragraph_index + 1}.{portion_index + 1}"
if portion_index == 0:
portion.portion_format.fill_format.fill_type = slides.FillType.SOLID
portion.portion_format.fill_format.solid_fill_color.color = draw.Color.red
portion.portion_format.font_bold = slides.NullableBool.TRUE
portion.portion_format.font_height = 15
elif portion_index == 1:
portion.portion_format.fill_format.fill_type = slides.FillType.SOLID
portion.portion_format.fill_format.solid_fill_color.color = draw.Color.blue
portion.portion_format.font_italic = slides.NullableBool.TRUE
portion.portion_format.font_height = 18
presentation.save("paragraphs_with_portions.pptx", slides.export.SaveFormat.PPTX)
글머리표 및 번호 매기기 목록 만들기
글머리표 또는 번호 매기기 목록 만들기
글머리표와 번호 매기기는 관련 항목을 더 쉽게 스캔할 수 있게 합니다. Aspose.Slides 에서는 BulletFormat 을 통해 목록 설정을 정의합니다.
- Presentation 클래스의 인스턴스를 생성합니다.
- 인덱스로 해당 슬라이드에 접근합니다.
- 선택한 슬라이드에 AutoShape 을 추가합니다.
- 도형의 TextFrame 에 접근합니다.
- 텍스트 프레임에서 기본 문단을 제거합니다.
- 기호 글머리표용 Paragraph 를 생성합니다.
- BulletFormat.type 을 BulletType.SYMBOL 로 설정하고 글머리 기호 문자를 지정합니다.
- 문단 텍스트, 들여쓰기, 글머리 색 및 글머리 높이를 설정합니다.
- 문단을 텍스트 프레임에 추가합니다.
- 두 번째 문단을 생성하고 BulletFormat.type 을 BulletType.NUMBERED 로 설정합니다.
- 번호 매기기 스타일을 구성하고 문단을 텍스트 프레임에 추가합니다.
- 프레젠테이션을 저장합니다.
이 Python 예제는 기호 글머리와 번호 매기기 글머리표를 생성합니다:
import aspose.pydrawing as draw
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 200, 200, 400, 200)
text_frame = shape.text_frame
text_frame.paragraphs.clear()
symbol_paragraph = slides.Paragraph()
symbol_paragraph.text = "Welcome to Aspose.Slides"
symbol_paragraph.paragraph_format.bullet.type = slides.BulletType.SYMBOL
symbol_paragraph.paragraph_format.bullet.char = chr(0x2022)
symbol_paragraph.paragraph_format.indent = 25
symbol_paragraph.paragraph_format.bullet.color.color_type = slides.ColorType.RGB
symbol_paragraph.paragraph_format.bullet.color.color = draw.Color.black
symbol_paragraph.paragraph_format.bullet.is_bullet_hard_color = slides.NullableBool.TRUE
symbol_paragraph.paragraph_format.bullet.height = 100
text_frame.paragraphs.add(symbol_paragraph)
numbered_paragraph = slides.Paragraph()
numbered_paragraph.text = "This is a numbered item"
numbered_paragraph.paragraph_format.bullet.type = slides.BulletType.NUMBERED
numbered_paragraph.paragraph_format.bullet.numbered_bullet_style = slides.NumberedBulletStyle.BULLET_CIRCLE_NUM_WD_BLACK_PLAIN
numbered_paragraph.paragraph_format.indent = 25
numbered_paragraph.paragraph_format.bullet.color.color_type = slides.ColorType.RGB
numbered_paragraph.paragraph_format.bullet.color.color = draw.Color.black
numbered_paragraph.paragraph_format.bullet.is_bullet_hard_color = slides.NullableBool.TRUE
numbered_paragraph.paragraph_format.bullet.height = 100
text_frame.paragraphs.add(numbered_paragraph)
presentation.save("bulleted_and_numbered_list.pptx", slides.export.SaveFormat.PPTX)
그림 글머리표 사용
그림 글머리표를 사용하면 기호나 번호 대신 사용자 정의 이미지를 사용할 수 있습니다.
- Presentation 클래스의 인스턴스를 생성합니다.
- 인덱스로 해당 슬라이드에 접근합니다.
- AutoShape 을 추가하고 그 TextFrame 에 접근합니다.
- 텍스트 프레임에서 기본 문단을 제거합니다.
- 글머리 이미지를 로드하고 프레젠테이션의 이미지 컬렉션에 PPImage 로 추가합니다.
- Paragraph 을 만들고 텍스트를 설정합니다.
- BulletFormat.type 을 BulletType.PICTURE 로 설정합니다.
- BulletFormat.picture 에 이미지를 지정하고 글머리 높이를 설정합니다.
- 문단을 텍스트 프레임에 추가합니다.
- 수정된 프레젠테이션을 저장합니다.
이 Python 예제는 그림 글머리표를 생성합니다:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
with slides.Images.from_file("bullets.png") as bullet_image:
presentation_image = presentation.images.add_image(bullet_image)
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 200, 200, 400, 200)
text_frame = shape.text_frame
text_frame.paragraphs.clear()
paragraph = slides.Paragraph()
paragraph.text = "Welcome to Aspose.Slides"
paragraph.paragraph_format.bullet.type = slides.BulletType.PICTURE
paragraph.paragraph_format.bullet.picture.image = presentation_image
paragraph.paragraph_format.bullet.height = 100
text_frame.paragraphs.add(paragraph)
presentation.save("picture_bullet.pptx", slides.export.SaveFormat.PPTX)
presentation.save("picture_bullet.ppt", slides.export.SaveFormat.PPT)
다중 레벨 목록 만들기
ParagraphFormat.depth 를 설정하여 목록의 서로 다른 레벨에 문단을 배치합니다. 최상위 레벨은 0 깊이를 갖습니다.
- Presentation 을 만들고 슬라이드에 접근합니다.
- AutoShape 을 추가하고 해당 텍스트 프레임에서 기본 문단을 지웁니다.
- 네 개의 문단을 만들고 글머리 기호를 구성합니다.
- 각 문단의 ParagraphFormat.depth 값을
0,1,2,3으로 설정합니다. - 문단을 텍스트 프레임에 추가하고 프레젠테이션을 저장합니다.
이 Python 예제는 네 단계의 글머리 목록을 생성합니다:
import aspose.pydrawing as draw
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 200, 200, 400, 200)
text_frame = shape.text_frame
text_frame.paragraphs.clear()
first_paragraph = slides.Paragraph()
first_paragraph.text = "Content"
first_paragraph.paragraph_format.bullet.type = slides.BulletType.SYMBOL
first_paragraph.paragraph_format.bullet.char = chr(0x2022)
first_paragraph.paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
first_paragraph.paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.black
first_paragraph.paragraph_format.depth = 0
second_paragraph = slides.Paragraph()
second_paragraph.text = "Second level"
second_paragraph.paragraph_format.bullet.type = slides.BulletType.SYMBOL
second_paragraph.paragraph_format.bullet.char = "-"
second_paragraph.paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
second_paragraph.paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.black
second_paragraph.paragraph_format.depth = 1
third_paragraph = slides.Paragraph()
third_paragraph.text = "Third level"
third_paragraph.paragraph_format.bullet.type = slides.BulletType.SYMBOL
third_paragraph.paragraph_format.bullet.char = chr(0x2022)
third_paragraph.paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
third_paragraph.paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.black
third_paragraph.paragraph_format.depth = 2
fourth_paragraph = slides.Paragraph()
fourth_paragraph.text = "Fourth level"
fourth_paragraph.paragraph_format.bullet.type = slides.BulletType.SYMBOL
fourth_paragraph.paragraph_format.bullet.char = "-"
fourth_paragraph.paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
fourth_paragraph.paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.black
fourth_paragraph.paragraph_format.depth = 3
text_frame.paragraphs.add(first_paragraph)
text_frame.paragraphs.add(second_paragraph)
text_frame.paragraphs.add(third_paragraph)
text_frame.paragraphs.add(fourth_paragraph)
presentation.save("multilevel_list.pptx", slides.export.SaveFormat.PPTX)
번호 매기기 목록 시작 값을 사용자 지정값으로 설정
BulletFormat.numbered_bullet_start_with 을 사용하여 번호 매기기 문단의 초기 번호를 지정합니다.
- Presentation 을 만들고 슬라이드에 AutoShape 를 추가합니다.
- 도형의 텍스트 프레임에서 기본 문단을 제거합니다.
- 세 개의 번호 매기기 문단을 생성합니다.
- 각 문단에 대해 BulletFormat.numbered_bullet_start_with 을 각각
2,3,7로 설정합니다. - 문단을 텍스트 프레임에 추가하고 프레젠테이션을 저장합니다.
이 Python 예제는 각 문단에 사용자 지정 시작 번호를 지정합니다:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 200, 200, 400, 200)
text_frame = shape.text_frame
text_frame.paragraphs.clear()
first_paragraph = slides.Paragraph()
first_paragraph.text = "Start at 2"
first_paragraph.paragraph_format.bullet.type = slides.BulletType.NUMBERED
first_paragraph.paragraph_format.bullet.numbered_bullet_start_with = 2
text_frame.paragraphs.add(first_paragraph)
second_paragraph = slides.Paragraph()
second_paragraph.text = "Start at 3"
second_paragraph.paragraph_format.bullet.type = slides.BulletType.NUMBERED
second_paragraph.paragraph_format.bullet.numbered_bullet_start_with = 3
text_frame.paragraphs.add(second_paragraph)
third_paragraph = slides.Paragraph()
third_paragraph.text = "Start at 7"
third_paragraph.paragraph_format.bullet.type = slides.BulletType.NUMBERED
third_paragraph.paragraph_format.bullet.numbered_bullet_start_with = 7
text_frame.paragraphs.add(third_paragraph)
presentation.save("custom_numbered_list.pptx", slides.export.SaveFormat.PPTX)
문단 레이아웃 및 끝 속성 제어
첫 줄 들여쓰기 설정
ParagraphFormat.indent 속성을 사용하여 문단의 첫 줄 들여쓰기를 제어합니다. 이 속성은 문단의 왼쪽 여백에 대해 첫 줄만 이동시킵니다. 양수 값은 첫 줄을 오른쪽으로 이동시키고, 나머지 줄은 문단 본문에 맞춰 정렬됩니다.
전체 문단을 이동해야 할 경우에는 ParagraphFormat.margin_left 를 사용하고, 첫 줄만 이동하려면 ParagraphFormat.indent 를 사용합니다.
아래 예제는 여러 문단을 만들고 서로 다른 ParagraphFormat.indent 값을 적용하여 첫 줄 들여쓰기가 문단 레이아웃에 미치는 영향을 보여줍니다.
- Presentation 클래스의 인스턴스를 생성합니다.
- 대상 슬라이드에 접근합니다.
- 슬라이드에 사각형 AutoShape 을 추가합니다.
- 도형의 TextFrame 에 접근하고 기본 문단을 제거합니다.
- 여러 문단을 만든 뒤 각각 다른 ParagraphFormat.indent 값을 설정합니다.
- 문단을 텍스트 프레임에 추가합니다.
- 수정된 프레젠테이션을 저장합니다.
이 코드는 문단 들여쓰기 설정 방법을 보여줍니다:
import aspose.pydrawing as draw
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 50, 50, 420, 220)
shape.fill_format.fill_type = slides.FillType.NO_FILL
shape.line_format.fill_format.fill_type = slides.FillType.SOLID
shape.line_format.fill_format.solid_fill_color.color = draw.Color.gray
text_frame = shape.text_frame
text_frame.text_frame_format.autofit_type = slides.TextAutofitType.SHAPE
text_frame.paragraphs.clear()
first_paragraph = slides.Paragraph()
first_paragraph.text = "No first-line indent. Wrapped lines start at the same position as the first line."
first_paragraph.paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
first_paragraph.paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.black
first_paragraph.paragraph_format.margin_left = 20
first_paragraph.paragraph_format.indent = 0
second_paragraph = slides.Paragraph()
second_paragraph.text = "First-line indent of 20 points. The first line moves to the right, while wrapped lines remain aligned to the paragraph body."
second_paragraph.paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
second_paragraph.paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.black
second_paragraph.paragraph_format.margin_left = 20
second_paragraph.paragraph_format.indent = 20
third_paragraph = slides.Paragraph()
third_paragraph.text = "First-line indent of 40 points. This paragraph shows a larger first-line offset to make the effect easier to see."
third_paragraph.paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
third_paragraph.paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.black
third_paragraph.paragraph_format.margin_left = 20
third_paragraph.paragraph_format.indent = 40
text_frame.paragraphs.add(first_paragraph)
text_frame.paragraphs.add(second_paragraph)
text_frame.paragraphs.add(third_paragraph)
presentation.save("paragraph_indent.pptx", slides.export.SaveFormat.PPTX)
결과:

내어쓰기 설정
내어쓰기는 첫 줄이 나머지 줄보다 왼쪽에 시작되는 문단 레이아웃입니다. Aspose.Slides 에서는 ParagraphFormat.indent 속성을 사용해 이 효과를 만들 수 있습니다. indent 를 음수 값으로 설정하면 첫 줄이 문단 본문에 비해 왼쪽으로 이동합니다.
실제로는 ParagraphFormat.margin_left 가 문단 본문의 왼쪽 위치를 정의하고, ParagraphFormat.indent 가 해당 여백에 대한 첫 줄 위치를 정의합니다. 내어쓰기를 만들려면 양수 margin_left 와 음수 indent 를 설정합니다.
이 서식은 참고문헌, 인용, 용어집 항목 등 줄 바꿈이 문단 본문 아래에 정렬되어야 할 경우에 유용합니다.
- Presentation 클래스의 인스턴스를 생성합니다.
- 대상 슬라이드에 접근합니다.
- 슬라이드에 사각형 AutoShape 을 추가합니다.
- 도형의 TextFrame 에 접근하고 기본 문단을 제거합니다.
- 각 문단에 대해 양수 ParagraphFormat.margin_left 값을 설정합니다.
- 내어쓰기 효과를 만들기 위해 음수 ParagraphFormat.indent 값을 설정합니다.
- 문단을 텍스트 프레임에 추가합니다.
- 수정된 프레젠테이션을 저장합니다.
이 코드는 문단에 내어쓰기를 설정하는 방법을 보여줍니다:
import aspose.pydrawing as draw
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 50, 50, 420, 220)
shape.fill_format.fill_type = slides.FillType.NO_FILL
shape.line_format.fill_format.fill_type = slides.FillType.SOLID
shape.line_format.fill_format.solid_fill_color.color = draw.Color.gray
text_frame = shape.text_frame
text_frame.text_frame_format.autofit_type = slides.TextAutofitType.SHAPE
text_frame.paragraphs.clear()
first_paragraph = slides.Paragraph()
first_paragraph.text = "A hanging indent is created by combining a positive left margin with a negative indent. The first line starts to the left, while wrapped lines align with the paragraph body."
first_paragraph.paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
first_paragraph.paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.black
first_paragraph.paragraph_format.margin_left = 40
first_paragraph.paragraph_format.indent = -20
second_paragraph = slides.Paragraph()
second_paragraph.text = "This second example uses a deeper hanging indent so the difference between the first line and the wrapped lines is easier to compare."
second_paragraph.paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
second_paragraph.paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.black
second_paragraph.paragraph_format.margin_left = 60
second_paragraph.paragraph_format.indent = -30
text_frame.paragraphs.add(first_paragraph)
text_frame.paragraphs.add(second_paragraph)
presentation.save("hanging_indent.pptx", slides.export.SaveFormat.PPTX)
결과:

문단 끝 실행 속성 설정
Paragraph.end_paragraph_portion_format 속성은 문단 끝 표시의 서식을 제어합니다. 다음 예제는 두 번째 문단 끝 표시에 글꼴 크기와 라틴 글꼴을 지정합니다.
- Presentation 을 로드하고 슬라이드에 접근합니다.
- AutoShape 을 추가하고 기본 문단을 제거합니다.
- 두 개의 문단을 만들고 텍스트 Portion을 추가합니다.
- 두 번째 문단 끝 표시용 PortionFormat 을 생성합니다.
- PortionFormat.font_height 와 PortionFormat.latin_font 을 설정합니다.
- 해당 형식을 Paragraph.end_paragraph_portion_format 에 할당하고 프레젠테이션을 저장합니다.
import aspose.slides as slides
with slides.Presentation("Test.pptx") as presentation:
slide = presentation.slides[0]
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 10, 10, 200, 250)
text_frame = shape.text_frame
text_frame.paragraphs.clear()
first_paragraph = slides.Paragraph()
first_paragraph.portions.add(slides.Portion("Sample text"))
second_paragraph = slides.Paragraph()
second_paragraph.portions.add(slides.Portion("Sample text 2"))
end_paragraph_format = slides.PortionFormat()
end_paragraph_format.font_height = 48
end_paragraph_format.latin_font = slides.FontData("Times New Roman")
second_paragraph.end_paragraph_portion_format = end_paragraph_format
text_frame.paragraphs.add(first_paragraph)
text_frame.paragraphs.add(second_paragraph)
presentation.save("end_paragraph_format.pptx", slides.export.SaveFormat.PPTX)
렌더링된 라인 수 세기
줄 끝에서 자동 줄바꿈 및 구두점 제어와 관련된 문단 규칙은 Control Line Breaking 및 Control Hanging Punctuation 을 참조하십시오.
Paragraph.get_lines_count 를 사용하면 텍스트 레이아웃 후 문단이 차지하는 라인 수를 셀 수 있습니다(자동 줄바꿈 포함). 이는 프레젠테이션 템플릿에서 텍스트 길이와 레이아웃을 확인할 때 유용합니다.
문단은 TextFrame.paragraphs の 하나의 항목이며 여러 렌더링 라인을 차지할 수 있습니다. 문단 내 명시적 줄 바꿈은 새로운 라인을 강제로 만들지만 새로운 문단을 만들지는 않습니다. 자동 줄바꿈은 명시적 줄 바꿈을 삽입하지 않고 가용 너비에 따라 라인을 생성합니다. 따라서 문단 수나 줄 바꿈 문자 수만으로는 실제 렌더링 라인 수를 알 수 없습니다.
다음 예제는 텍스트 도형을 만들고 라인 수를 세고, 도형을 좁힌 뒤 텍스트를 짧은 문자열로 교체합니다. 줄바꿈은 활성화되고 자동 맞춤은 비활성화되어 도형 너비가 줄바꿈을 제어합니다. 도형 크기는 포인트 단위입니다. 마지막으로 또 다른 문단을 추가하고 텍스트 프레임 전체의 라인 수를 합산합니다.
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 50, 50, 400, 200)
text_frame = shape.text_frame
text_frame.text_frame_format.wrap_text = slides.NullableBool.TRUE
text_frame.text_frame_format.autofit_type = slides.TextAutofitType.NONE
paragraph = text_frame.paragraphs[0]
paragraph.paragraph_format.default_portion_format.font_height = 20
paragraph.text = "This text demonstrates how automatic wrapping changes the number of rendered lines."
print(f"Original width: {paragraph.get_lines_count()}")
shape.width = 150
print(f"Narrower shape: {paragraph.get_lines_count()}")
paragraph.text = "Short text."
print(f"Shorter text: {paragraph.get_lines_count()}")
second_paragraph = slides.Paragraph()
second_paragraph.text = "Another paragraph."
second_paragraph.paragraph_format.default_portion_format.font_height = 20
text_frame.paragraphs.add(second_paragraph)
total_line_count = 0
for current_paragraph in text_frame.paragraphs:
total_line_count += current_paragraph.get_lines_count()
print(f"Total lines in the text frame: {total_line_count}")
이 텍스트와 차원에서는 도형을 좁히면 라인 수가 증가하고, 짧은 문자열로 교체하면 라인 수가 감소합니다. 정확한 라인 수는 글꼴 가용성 및 대체, 글꼴 크기, 여백, 들여쓰기, 줄바꿈 및 자동 맞춤 설정에 따라 달라질 수 있습니다. 템플릿을 확인할 때는 대상 환경에 사용할 글꼴 및 레이아웃 설정을 적용하십시오.
라인 수만으로 텍스트가 컨테이너를 초과하는지 여부를 판단할 수 없습니다. 사용 가능한 높이, 라인 높이, 문단 및 라인 간격, 자동 맞춤 동작도 중요합니다; 자동 맞춤이 비활성화된 경우 단일 라인이라도 가용 너비를 초과할 수 있습니다.
문단 내용 가져오기 및 내보내기
HTML 텍스트를 문단에 가져오기
ParagraphCollection.add_from_html 를 사용하면 HTML 마크업을 텍스트 프레임의 문단과 Portion 으로 변환할 수 있습니다.
- Presentation 클래스의 인스턴스를 생성합니다.
- 슬라이드에 AutoShape 를 추가합니다.
- 도형의 TextFrame 에 접근하고 기본 문단을 지웁니다.
- 소스 HTML 파일을 읽습니다.
- HTML 문자열을 ParagraphCollection.add_from_html 에 전달합니다.
- 수정된 프레젠테이션을 저장합니다.
이 Python 예제는 HTML을 텍스트 프레임에 가져옵니다:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
shape_width = presentation.slide_size.size.width - 20
shape_height = presentation.slide_size.size.height - 20
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 10, 10, shape_width, shape_height)
shape.fill_format.fill_type = slides.FillType.NO_FILL
shape.text_frame.paragraphs.clear()
with open("file.html", "r", encoding="utf-8") as html_stream:
html = html_stream.read()
shape.text_frame.paragraphs.add_from_html(html)
presentation.save("html_text.pptx", slides.export.SaveFormat.PPTX)
문단 텍스트를 HTML로 내보내기
ParagraphCollection.export_to_html 를 사용하면 선택한 범위의 문단을 HTML 로 내보낼 수 있습니다.
- Presentation 인스턴스를 생성하고 원하는 프레젠테이션을 로드합니다.
- 슬라이드에 접근하고 텍스트를 포함한 AutoShape 을 찾습니다.
- 도형의 TextFrame 에 접근합니다.
- 시작 문단 인덱스와 내보낼 문단 수를 지정하여 ParagraphCollection.export_to_html 을 호출합니다.
- 반환된 HTML 문자열을 파일에 기록합니다.
이 Python 예제는 첫 번째 텍스트 도형의 모든 문단을 내보냅니다:
import aspose.slides as slides
with slides.Presentation("ExportingHTMLText.pptx") as presentation:
shape = presentation.slides[0].shapes[0]
if isinstance(shape, slides.AutoShape) and shape.text_frame is not None:
paragraphs = shape.text_frame.paragraphs
html = paragraphs.export_to_html(0, paragraphs.count, None)
with open("paragraphs.html", "w", encoding="utf-8") as html_stream:
html_stream.write(html)
else:
print("The first shape is not a text shape.")
문단을 이미지로 렌더링
Paragraph 은 개별 문단을 직접 렌더링하는 get_image 메서드를 제공합니다. 이 메서드는 IImage 을 반환하며, IImage.save 로 파일이나 스트림에 저장할 수 있습니다. 포함된 도형을 렌더링하거나 비트맵을 수동으로 자를 필요가 없습니다.
get_image 메서드는 문단이 상위 컬렉션에 없거나 유효한 렌더링 경계가 없거나 렌더링할 수 없는 경우 None 을 반환할 수 있습니다. 저장하기 전에 결과를 확인하고, 반환된 이미지를 컨텍스트 매니저로 사용하여 리소스를 해제하십시오.
기본 배율로 문단 렌더링
sample.pptx 라는 프레젠테이션 파일에 슬라이드가 하나 있고, 첫 번째 도형이 세 개의 문단을 포함한 텍스트 상자라고 가정합니다.

다음 예제는 두 번째 문단을 기본 배율로 렌더링하고 PNG 형식으로 저장합니다:
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
shape = presentation.slides[0].shapes[0]
if isinstance(shape, slides.AutoShape) and shape.text_frame is not None and shape.text_frame.paragraphs.count > 1:
paragraph = shape.text_frame.paragraphs[1]
paragraph_image = paragraph.get_image()
if paragraph_image is not None:
with paragraph_image:
paragraph_image.save("paragraph.png", slides.ImageFormat.PNG)
else:
print("The paragraph could not be rendered.")
else:
print("The expected text shape or paragraph was not found.")
결과:

테이블 셀 내 문단을 스케일링하여 렌더링
get_image 에 가로·세로 스케일 팩터를 전달하여 렌더링된 문단의 크기를 제어합니다. 다음 예제는 표를 만들고 첫 번째 셀의 문단을 기본 너비와 높이의 두 배로 렌더링한 뒤 PNG 이미지로 저장합니다:
import aspose.slides as slides
scale_x = 2
scale_y = 2
with slides.Presentation() as presentation:
slide = presentation.slides[0]
table = slide.shapes.add_table(50, 50, [300], [80])
paragraph = table.rows[0][0].text_frame.paragraphs[0]
paragraph.text = "Text in a table cell"
paragraph_image = paragraph.get_image(scale_x, scale_y)
if paragraph_image is not None:
with paragraph_image:
paragraph_image.save("table_paragraph.png", slides.ImageFormat.PNG)
else:
print("The paragraph could not be rendered.")
스케일 팩터 1 은 해당 축을 기본 픽셀 크기로 유지합니다. 예를 들어 두 축 모두 2 로 지정하면 이미지의 너비와 높이가 대략 두 배가 되어 픽셀 수는 네 배가 됩니다. 큰 팩터는 확대하거나 고해상도 출력 시 텍스트를 더 선명하게 만들지만 메모리 사용량과 파일 크기도 증가합니다. 1 이하의 팩터는 세부 사항이 적은 작은 이미지를 생성합니다. 비율을 유지하려면 가로와 세로 팩터를 동일하게 사용하고, 서로 다른 팩터를 사용하면 출력이 각각 독립적으로 늘어납니다.
도형 전체를 렌더링하려면 Shape.get_image 를 사용하는 것이 유용합니다(도형의 채우기, 테두리 등 시각적 컨텍스트 포함). 문단 전용 이미지가 필요할 경우 Paragraph.get_image 를 사용하십시오.
FAQ
텍스트 프레임 내부의 줄 바꿈을 완전히 비활성화할 수 있나요?
예. TextFrameFormat.wrap_text 를 설정하면 줄 바꿈이 비활성화되어 텍스트 프레임 가장자리를 기준으로 줄이 끊기지 않습니다.
특정 문단의 정확한 슬라이드 상 위치를 어떻게 얻나요?
Paragraph.get_rect 를 사용하면 문단의 경계 사각형을 가져올 수 있습니다. Portion.get_rect 은 개별 Portion의 경계를 제공합니다.
문단 정렬(왼쪽, 오른쪽, 가운데, 양쪽 정렬)은 어디서 제어되나요?
ParagraphFormat.alignment 은 문단 수준 설정이며, 개별 Portion 서식에 관계없이 전체 문단에 적용됩니다.
각 라인 안에서 서로 다른 글꼴 크기의 Portion을 수직 정렬하려면 Align Fonts Within a Line 를参照하십시오.
문단의 일부분에 교정 언어를 설정할 수 있나요?
예. 개별 Portion에 대해 PortionFormat.language_id 를 설정하면 하나의 문단에 여러 언어의 텍스트를 포함할 수 있습니다.