مدیریت پاراگراف‌های متنی پاورپوینت در پایتون از طریق جاوا

مرور کلی

Aspose.Slides for Python via Java متن را به صورت یک سلسله مراتب از چارچوب‌های متن، پاراگراف‌ها و بخش‌ها نمایش می‌دهد:

  • TextFrame نمایانگر محفظه متن در یک شکل است و دسترسی به مجموعه پاراگراف‌های آن را فراهم می‌کند.
  • Paragraph نشان‌دهنده یک پاراگراف در یک چارچوب متن است و دسترسی به بخش‌ها و فرمت‌بندی سطح پاراگراف را می‌دهد.
  • Portion نمایانگر یک اجرای متنی درون یک پاراگراف است. هر بخش می‌تواند متن و فرمت‌بندی کاراکتری خاص خود را داشته باشد.

بنابراین یک پاراگراف می‌تواند متنی با فونت‌ها، رنگ‌ها، اندازه‌ها و فرمت‌های دیگر مختلف داشته باشد با استفاده از چندین بخش.

ایجاد و فرمت‌بندی پاراگراف‌ها

پاراگراف‌ها را در چارچوب متنی یک شکل ایجاد کنید و از بخش‌ها برای فرمت‌بندی اجرای‌های متنی جداگانه استفاده کنید. این بخش نشان می‌دهد چگونه چندین بخش را در هر پاراگراف ترکیب کنید و رنگ‌ها، اندازه‌های فونت و سبک‌های مختلف را اعمال کنید.

ایجاد پاراگراف‌ها با چندین بخش

مثال پایتون زیر سه پاراگراف ایجاد می‌کند که هر یک شامل سه اجرای متنی در مجموعه‌ای هستند که توسط Paragraph.getPortions بازگردانده می‌شود. این مثال پاراگراف پیش‌فرض و بخش خالی آن را دوباره استفاده می‌کند، سپس پاراگراف‌ها و بخش‌های باقی‌مانده را اضافه می‌کند. فرمت‌بندی از طریق Portion.getPortionFormat اعمال می‌شود: اولین بخش در هر پاراگراف قرمز، پررنگ و 15 پوینت است؛ دومین بخش آبی، ایتالیک و 18 پوینت؛ بخش سوم فرمت پیش‌فرض خود را حفظ می‌کند:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FillType, NullableBool, Paragraph, Portion, Presentation, SaveFormat, ShapeType
from java.awt import Color

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 150, 300, 150)
    text_frame = shape.getTextFrame()

    first_paragraph = text_frame.getParagraphs().get_Item(0)
    first_paragraph.getPortions().add(Portion())
    first_paragraph.getPortions().add(Portion())

    second_paragraph = Paragraph()
    second_paragraph.getPortions().add(Portion())
    second_paragraph.getPortions().add(Portion())
    second_paragraph.getPortions().add(Portion())
    text_frame.getParagraphs().add(second_paragraph)

    third_paragraph = Paragraph()
    third_paragraph.getPortions().add(Portion())
    third_paragraph.getPortions().add(Portion())
    third_paragraph.getPortions().add(Portion())
    text_frame.getParagraphs().add(third_paragraph)

    paragraph_count = text_frame.getParagraphs().getCount()
    for paragraph_index in range(paragraph_count):
        paragraph = text_frame.getParagraphs().get_Item(paragraph_index)
        portion_count = paragraph.getPortions().getCount()
        for portion_index in range(portion_count):
            portion = paragraph.getPortions().get_Item(portion_index)
            portion.setText(f"Portion {paragraph_index + 1}.{portion_index + 1}")

            if portion_index == 0:
                portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid)
                portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.RED)
                portion.getPortionFormat().setFontBold(NullableBool.True_)
                portion.getPortionFormat().setFontHeight(15)
            elif portion_index == 1:
                portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid)
                portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLUE)
                portion.getPortionFormat().setFontItalic(NullableBool.True_)
                portion.getPortionFormat().setFontHeight(18)

    presentation.save("paragraphs_with_portions.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

ایجاد فهرست‌های بولت‌دار و شماره‌دار

متن پاراگراف‌ها را به فهرست‌هایی با نمادها، اعداد یا تصاویر سفارشی سازماندهی کنید. مثال‌های این بخش شامل فرمت‌بندی بولت، فهرست‌های چندسطحی و تنظیم اعداد آغازین سفارشی است.

ایجاد فهرست بولت‌دار یا شماره‌دار

بولت‌ها و شماره‌گذاری موارد مرتبط را برای اسکن آسان‌تر می‌سازند. در Aspose.Slides تنظیمات فهرست از طریق BulletFormat تعریف می‌شود.

این مثال پایتون یک بولت نماد و یک مورد شماره‌دار را به همان چارچوب متنی اضافه می‌کند. BulletFormat.setType نوع بولت را انتخاب می‌کند، در حالی که BulletFormat.setChar نماد را فراهم می‌کند و BulletFormat.setNumberedBulletStyle سبک شماره‌گذاری را انتخاب می‌کند. هر دو مورد از بولت‌های سیاه با ارتفاع 100 درصد و تورفتگی پاراگراف 25 پوینت استفاده می‌کنند:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import BulletType, ColorType, NullableBool, NumberedBulletStyle, Paragraph, Presentation, SaveFormat, ShapeType
from java.awt import Color

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200)
    text_frame = shape.getTextFrame()
    text_frame.getParagraphs().clear()

    symbol_paragraph = Paragraph()
    symbol_paragraph.setText("Welcome to Aspose.Slides")
    symbol_paragraph.getParagraphFormat().getBullet().setType(BulletType.Symbol)
    symbol_paragraph.getParagraphFormat().getBullet().setChar("•")
    symbol_paragraph.getParagraphFormat().setIndent(25)
    symbol_paragraph.getParagraphFormat().getBullet().getColor().setColorType(ColorType.RGB)
    symbol_paragraph.getParagraphFormat().getBullet().getColor().setColor(Color.BLACK)
    symbol_paragraph.getParagraphFormat().getBullet().setBulletHardColor(NullableBool.True_)
    symbol_paragraph.getParagraphFormat().getBullet().setHeight(100)
    text_frame.getParagraphs().add(symbol_paragraph)

    numbered_paragraph = Paragraph()
    numbered_paragraph.setText("This is a numbered item")
    numbered_paragraph.getParagraphFormat().getBullet().setType(BulletType.Numbered)
    numbered_paragraph.getParagraphFormat().getBullet().setNumberedBulletStyle(NumberedBulletStyle.BulletCircleNumWDBlackPlain)
    numbered_paragraph.getParagraphFormat().setIndent(25)
    numbered_paragraph.getParagraphFormat().getBullet().getColor().setColorType(ColorType.RGB)
    numbered_paragraph.getParagraphFormat().getBullet().getColor().setColor(Color.BLACK)
    numbered_paragraph.getParagraphFormat().getBullet().setBulletHardColor(NullableBool.True_)
    numbered_paragraph.getParagraphFormat().getBullet().setHeight(100)
    text_frame.getParagraphs().add(numbered_paragraph)

    presentation.save("bulleted_and_numbered_list.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

استفاده از بولت‌های تصویری

بولت‌های تصویری به شما امکان می‌دهند به جای نماد یا عدد، از تصویر سفارشی استفاده کنید.

این مثال پایتون یک تصویر را به مجموعه تصاویر ارائه به عنوان یک PPImage اضافه می‌کند و از آن به عنوان بولت پاراگراف استفاده می‌کند. برای استفاده از بولت تصویری BulletFormat.setType فراخوانی می‌شود و تصویر از طریق BulletFormat.getPicture انتساب می‌یابد. BulletFormat.setHeight به 100 درصد تنظیم می‌شود تا بولت نسبت به متن پاراگراف مقیاس یابد:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import BulletType, Images, Paragraph, Presentation, SaveFormat, ShapeType

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    bullet_image = Images.fromFile("bullets.png")
    try:
        presentation_image = presentation.getImages().addImage(bullet_image)
    finally:
        bullet_image.dispose()

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200)
    text_frame = shape.getTextFrame()
    text_frame.getParagraphs().clear()

    paragraph = Paragraph()
    paragraph.setText("Welcome to Aspose.Slides")
    paragraph.getParagraphFormat().getBullet().setType(BulletType.Picture)
    paragraph.getParagraphFormat().getBullet().getPicture().setImage(presentation_image)
    paragraph.getParagraphFormat().getBullet().setHeight(100)
    text_frame.getParagraphs().add(paragraph)

    presentation.save("picture_bullet.pptx", SaveFormat.Pptx)
    presentation.save("picture_bullet.ppt", SaveFormat.Ppt)
finally:
    presentation.dispose()

ایجاد فهرست چندسطحی

ParagraphFormat.setDepth را تنظیم کنید تا پاراگراف‌ها را در سطوح مختلف فهرست قرار دهید. سطح بالایی دارای عمق 0 است.

این مثال پایتون مقادیر ParagraphFormat.setDepth را از 0 تا 3 به چهار پاراگراف اختصاص می‌دهد و یک سلسله مراتب داخل یک چارچوب متنی ایجاد می‌کند. هر پاراگراف از بولت نماد استفاده می‌کند و BulletFormat.setChar بین یک بولت گرد و یک خط تیره تغییر می‌کند تا سطوح مجاور را متمایز سازد:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import BulletType, FillType, Paragraph, Presentation, SaveFormat, ShapeType
from java.awt import Color

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200)
    text_frame = shape.getTextFrame()
    text_frame.getParagraphs().clear()

    first_paragraph = Paragraph()
    first_paragraph.setText("Content")
    first_paragraph.getParagraphFormat().getBullet().setType(BulletType.Symbol)
    first_paragraph.getParagraphFormat().getBullet().setChar("•")
    first_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
    first_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
    first_paragraph.getParagraphFormat().setDepth(0)

    second_paragraph = Paragraph()
    second_paragraph.setText("Second level")
    second_paragraph.getParagraphFormat().getBullet().setType(BulletType.Symbol)
    second_paragraph.getParagraphFormat().getBullet().setChar('-')
    second_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
    second_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
    second_paragraph.getParagraphFormat().setDepth(1)

    third_paragraph = Paragraph()
    third_paragraph.setText("Third level")
    third_paragraph.getParagraphFormat().getBullet().setType(BulletType.Symbol)
    third_paragraph.getParagraphFormat().getBullet().setChar("•")
    third_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
    third_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
    third_paragraph.getParagraphFormat().setDepth(2)

    fourth_paragraph = Paragraph()
    fourth_paragraph.setText("Fourth level")
    fourth_paragraph.getParagraphFormat().getBullet().setType(BulletType.Symbol)
    fourth_paragraph.getParagraphFormat().getBullet().setChar('-')
    fourth_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
    fourth_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
    fourth_paragraph.getParagraphFormat().setDepth(3)

    text_frame.getParagraphs().add(first_paragraph)
    text_frame.getParagraphs().add(second_paragraph)
    text_frame.getParagraphs().add(third_paragraph)
    text_frame.getParagraphs().add(fourth_paragraph)

    presentation.save("multilevel_list.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

شروع موارد فهرست شماره‌دار با مقادیر سفارشی

از BulletFormat.setNumberedBulletStartWith برای تعیین عدد اولیه نمایش داده‌شده برای یک پاراگراف شماره‌دار استفاده کنید.

این مثال پایتون BulletFormat.setType را برای استفاده از شماره‌گذاری برای سه پاراگراف تنظیم می‌کند و مقادیر BulletFormat.setNumberedBulletStartWith را به ترتیب 2، 3 و 7 انتساب می‌دهد. مقدار شروع به‌صورت جداگانه برای هر پاراگراف پیکربندی می‌شود و اجازه می‌دهد اعداد نمایش داده‌شده مقادیر را رد کنند:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import BulletType, Paragraph, Presentation, SaveFormat, ShapeType

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200)
    text_frame = shape.getTextFrame()
    text_frame.getParagraphs().clear()

    first_paragraph = Paragraph()
    first_paragraph.setText("Start at 2")
    first_paragraph.getParagraphFormat().getBullet().setType(BulletType.Numbered)
    first_paragraph.getParagraphFormat().getBullet().setNumberedBulletStartWith(2)

    text_frame.getParagraphs().add(first_paragraph)

    second_paragraph = Paragraph()
    second_paragraph.setText("Start at 3")
    second_paragraph.getParagraphFormat().getBullet().setType(BulletType.Numbered)
    second_paragraph.getParagraphFormat().getBullet().setNumberedBulletStartWith(3)
    text_frame.getParagraphs().add(second_paragraph)

    third_paragraph = Paragraph()
    third_paragraph.setText("Start at 7")
    third_paragraph.getParagraphFormat().getBullet().setType(BulletType.Numbered)
    third_paragraph.getParagraphFormat().getBullet().setNumberedBulletStartWith(7)
    text_frame.getParagraphs().add(third_paragraph)

    presentation.save("custom_numbered_list.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

کنترل چیدمان پاراگراف و ویژگی‌های انتهایی

حاشیه‌ها و تورفتگی‌های پاراگراف را تنظیم کنید تا موقعیت خط اول و خطوط بسته‌شده را کنترل کنید. همچنین می‌توانید علامت پایان پاراگراف را جدا از بخش‌های متنی قابل رؤیت فرمت‌بندی کنید.

تنظیم تورفتگی خط اول

از متد ParagraphFormat.setIndent برای کنترل تورفتگی خط اول یک پاراگراف استفاده کنید. این تنظیم تنها خط اول را نسبت به حاشیه چپ پاراگراف جابه‌جا می‌کند. مقدار مثبت خط اول را به سمت راست می‌برد، در حالی که خطوط باقی‌مانده به بدنه پاراگراف هم‌راست می‌مانند.

از ParagraphFormat.setMarginLeft هنگامی که نیاز به جابه‌جایی کل پاراگراف دارید استفاده کنید. از ParagraphFormat.setIndent وقتی فقط خط اول را می‌خواهید جابجا کنید، استفاده کنید.

مثال زیر سه پاراگراف را با همان ParagraphFormat.setMarginLeft برابر 20 پوینت و مقادیر ParagraphFormat.setIndent برابر 0، 20 و 40 پوینت تنظیم می‌کند. خطوط اول با جابجایی‌های بزرگتر شروع می‌شوند، در حالی که خطوط بسته‌شده همان حاشیه چپ را حفظ می‌کنند:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FillType, Paragraph, Presentation, SaveFormat, ShapeType, TextAutofitType
from java.awt import Color

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 420, 220)
    shape.getFillFormat().setFillType(FillType.NoFill)
    shape.getLineFormat().getFillFormat().setFillType(FillType.Solid)
    shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY)

    text_frame = shape.getTextFrame()
    text_frame.getTextFrameFormat().setAutofitType(TextAutofitType.Shape)
    text_frame.getParagraphs().clear()

    first_paragraph = Paragraph()
    first_paragraph.setText("No first-line indent. Wrapped lines start at the same position as the first line.")
    first_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
    first_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
    first_paragraph.getParagraphFormat().setMarginLeft(20.0)
    first_paragraph.getParagraphFormat().setIndent(0.0)

    second_paragraph = Paragraph()
    second_paragraph.setText("First-line indent of 20 points. The first line moves to the right, while wrapped lines remain aligned to the paragraph body.")
    second_paragraph.getParagraphFormat().getDefaultPortionFormat().setFillType(FillType.Solid)
    second_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
    second_paragraph.getParagraphFormat().setMarginLeft(20.0)
    second_paragraph.getParagraphFormat().setIndent(20.0)

    third_paragraph = Paragraph()
    third_paragraph.setText("First-line indent of 40 points. This paragraph shows a larger first-line offset to make the effect easier to see.")
    third_paragraph.getParagraphFormat().getDefaultPortionFormat().setFillType(FillType.Solid)
    third_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
    third_paragraph.getParagraphFormat().setMarginLeft(20.0)
    third_paragraph.getParagraphFormat().setIndent(40.0)

    text_frame.getParagraphs().add(first_paragraph)
    text_frame.getParagraphs().add(second_paragraph)
    text_frame.getParagraphs().add(third_paragraph)

    presentation.save("paragraph_indent.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

نتیجه:

The first-line indent of the paragraphs

تنظیم تورفتگی معلق

تورفتگی معلق یک چیدمان پاراگراف است که در آن خط اول سمت چپ خطوط باقی‌مانده شروع می‌شود. در Aspose.Slides این اثر را با ویژگی ParagraphFormat.setIndent ایجاد می‌کنید. مقدار Indent را به مقدار منفی تنظیم کنید تا خط اول نسبت به بدنه پاراگراف به سمت چپ جابه‌جا شود.

در عمل، ParagraphFormat.setMarginLeft موقعیت چپ بدنه پاراگراف را تعریف می‌کند و ParagraphFormat.setIndent موقعیت خط اول را نسبت به آن حاشیه تعیین می‌کند. برای ایجاد تورفتگی معلق، مقدار MarginLeft را مثبت و مقدار Indent را منفی تنظیم کنید.

این فرمت‌بندی برای فهرست‌های کتاب‌شناسی، مراجع، ورودی‌های واژه‌نامه و سایر پاراگراف‌هایی که خطوط بسته‌شده باید زیر بدنه پاراگراف نه زیر اولین کاراکتر خط اول هم‌راست شوند، مفید است.

مثال پایتون زیر مقادیر ParagraphFormat.setMarginLeft برابر 40 و 60 پوینت را با مقادیر ParagraphFormat.setIndent برابر -20 و -30 پوینت ترکیب می‌کند. در هر پاراگراف، خط اول سمت چپ خطوط بسته‌شده شروع می‌شود؛ پاراگراف دوم تورفتگی معلق عمیق‌تری دارد:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FillType, Paragraph, Presentation, SaveFormat, ShapeType, TextAutofitType
from java.awt import Color

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 420, 220)
    shape.getFillFormat().setFillType(FillType.NoFill)
    shape.getLineFormat().getFillFormat().setFillType(FillType.Solid)
    shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY)

    text_frame = shape.getTextFrame()
    text_frame.getTextFrameFormat().setAutofitType(TextAutofitType.Shape)
    text_frame.getParagraphs().clear()

    first_paragraph = Paragraph()
    first_paragraph.setText("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.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
    first_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
    first_paragraph.getParagraphFormat().setMarginLeft(40.0)
    first_paragraph.getParagraphFormat().setIndent(-20.0)

    second_paragraph = Paragraph()
    second_paragraph.setText("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.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
    second_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
    second_paragraph.getParagraphFormat().setMarginLeft(60.0)
    second_paragraph.getParagraphFormat().setIndent(-30.0)

    text_frame.getParagraphs().add(first_paragraph)
    text_frame.getParagraphs().add(second_paragraph)

    presentation.save("hanging_indent.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

نتیجه:

The hanging indent of the paragraphs

تنظیم ویژگی‌های انتهای پاراگراف

متد Paragraph.setEndParagraphPortionFormat فرمت علامت پایان پاراگراف را کنترل می‌کند. مثال زیر یک PortionFormat ایجاد می‌کند که در آن BasePortionFormat.setFontHeight برابر 48 پوینت و BasePortionFormat.setLatinFont برابر Times New Roman تنظیم شده‌اند. این فرمت به علامت پایان پاراگراف دوم انتساب می‌شود و بخش‌های متنی قابل رؤیت دست نخورده می‌مانند:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FontData, Paragraph, Portion, PortionFormat, Presentation, SaveFormat, ShapeType

presentation = Presentation("Test.pptx")
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 10, 10, 200, 250)
    text_frame = shape.getTextFrame()
    text_frame.getParagraphs().clear()

    first_paragraph = Paragraph()
    first_portion = Portion("Sample text")
    first_paragraph.getPortions().add(first_portion)

    second_paragraph = Paragraph()
    second_portion = Portion("Sample text 2")
    second_paragraph.getPortions().add(second_portion)

    end_paragraph_format = PortionFormat()
    end_paragraph_format.setFontHeight(48)
    latin_font = FontData("Times New Roman")
    end_paragraph_format.setLatinFont(latin_font)
    second_paragraph.setEndParagraphPortionFormat(end_paragraph_format)

    text_frame.getParagraphs().add(first_paragraph)
    text_frame.getParagraphs().add(second_paragraph)

    presentation.save("end_paragraph_format.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

شمارش خطوط رندر شده

برای قواعد پاراگرافی که بر بسته شدن خودکار و نقطه‌گذاری در انتهای خطوط تاثیر دارند، به Control Line Breaking و Control Hanging Punctuation مراجعه کنید.

از Paragraph.getLinesCount برای شمارش خطوطی که یک پاراگراف پس از چینش متن اشغال می‌کند، شامل بسته شدن خودکار، استفاده کنید. این برای بررسی طول متن و چینش در قالب‌های ارائه مفید است.

یک پاراگراف یک مورد در TextFrame.getParagraphs است و می‌تواند چندین خط رندر شده اشغال کند. شکست خط صریح داخل یک پاراگراف خط جدیدی ایجاد می‌کند بدون اینکه پاراگراف دیگری ساخته شود. بسته شدن خودکار خطوط را بر پایه عرض موجود ایجاد می‌کند بدون این‌که شکست‌های صریح به متن اضافه شود. بنابراین شمارش پاراگراف‌ها یا کاراکترهای شکست خط، شمارش خطوط رندر شده را نمی‌دهد.

مثال زیر یک شکل متنی ایجاد می‌کند، خطوط آن را می‌شمارد، شکل را باریک می‌کند و سپس متن را با رشته‌ای کوتاه‌تر جایگزین می‌کند. این مثال با استفاده از TextFrameFormat.setWrapText بسته شدن متن را فعال می‌کند و با TextFrameFormat.setAutofitType خودکار شدن را غیرفعال می‌کند تا عرض شکل کنترل بسته شدن را بدون کوچک‌سازی خودکار متن یا تغییر اندازه شکل انجام دهد. هر اندازه‌گیری با Paragraph.getLinesCount انجام می‌شود. ابعاد شکل بر حسب پوینت هستند. در نهایت، مثال یک پاراگراف دیگر اضافه می‌کند و شمارش خطوط را در تمام چارچوب متن جمع می‌کند.

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import NullableBool, Paragraph, Presentation, ShapeType, TextAutofitType

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 400, 200)
    text_frame = shape.getTextFrame()
    text_frame.getTextFrameFormat().setWrapText(NullableBool.True_)
    text_frame.getTextFrameFormat().setAutofitType(TextAutofitType.None_)

    paragraph = text_frame.getParagraphs().get_Item(0)
    paragraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(20)
    paragraph.setText("This text demonstrates how automatic wrapping changes the number of rendered lines.")
    print("Original width:", paragraph.getLinesCount())

    shape.setWidth(150)
    print("Narrower shape:", paragraph.getLinesCount())

    paragraph.setText("Short text.")
    print("Shorter text:", paragraph.getLinesCount())

    second_paragraph = Paragraph()
    second_paragraph.setText("Another paragraph.")
    second_paragraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(20)
    text_frame.getParagraphs().add(second_paragraph)

    total_line_count = 0
    for current_paragraph in text_frame.getParagraphs():
        total_line_count += current_paragraph.getLinesCount()
    print("Total lines in the text frame:", total_line_count)
finally:
    presentation.dispose()

با این متن و این ابعاد، باریک کردن شکل تعداد خطوط را افزایش می‌دهد، در حالی که جایگزینی متن با رشته کوتاه‌تر آن را کاهش می‌دهد. شمارش دقیق می‌تواند با در دسترس بودن فونت، جایگزینی، اندازه فونت، حاشیه‌ها، تورفتگی، بسته شدن و تنظیمات خودکار شدن متفاوت باشد؛ از فونت‌ها و تنظیمات چیدمان موردنظر برای محیط هدف هنگام بررسی قالب استفاده کنید.

تنها شمارش خطوط تعیین‌کننده این نیست که آیا متن از مخزن خود عبور می‌کند یا نه. ارتفاع موجود، ارتفاع خطوط، فاصله بین پاراگراف‌ها و خطوط، و رفتار خودکار شدن نیز مهم هستند؛ حتی یک خط می‌تواند عرض موجود را در صورت غیرفعال بودن بسته شدن پشت سر بگذارد.

واردات و صادرات محتوای پاراگراف

از HTML برای آوردن متن به یک ارائه یا صادرات پاراگراف‌های موجود برای استفاده خارج از آن استفاده کنید. این بخش همچنین نشان می‌دهد چگونه پاراگراف‌های منفرد را به عنوان تصویر رندر کنید، از جمله پاراگراف‌های موجود در سلول‌های جدول.

وارد کردن متن HTML به پاراگراف‌ها

از ParagraphCollection.addFromHtml برای تبدیل نشانه‌گذاری HTML به پاراگراف‌ها و بخش‌ها در یک چارچوب متنی استفاده کنید.

این مثال پایتون HTML را از یک فایل می‌خواند و نشانه‌گذاری را به ParagraphCollection.addFromHtml پاس می‌دهد. پاراگراف پیش‌فرض چارچوب متنی را پاک می‌کند تا فقط محتوای واردشده در چارچوب باشد. شکل مستطیلی حاشیه‌ای به اندازه 10 پوینت در هر طرف اسلاید ایجاد می‌کند:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import FillType, Presentation, SaveFormat, ShapeType
from pathlib import Path

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    shape_width = presentation.getSlideSize().getSize().getWidth() - 20
    shape_height = presentation.getSlideSize().getSize().getHeight() - 20

    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 10, 10, shape_width, shape_height)
    shape.getFillFormat().setFillType(FillType.NoFill)
    shape.getTextFrame().getParagraphs().clear()

    try:
        html = Path("file.html").read_text(encoding="utf-8")
        shape.getTextFrame().getParagraphs().addFromHtml(html)

        presentation.save("html_text.pptx", SaveFormat.Pptx)
    except OSError as exception:
        print("The HTML file could not be read: " + str(exception))
finally:
    presentation.dispose()

صادرات متن پاراگراف به HTML

از ParagraphCollection.exportToHtml برای صادرات یک محدوده انتخابی از پاراگراف‌ها به HTML استفاده کنید. زمانی که متن ارائه را در یک وب‌سایت دوباره استفاده می‌کنید، ممکن است بخواهید تأکید محلی را بدون انتقال فونت، اندازه یا سبک‌های جعبه متن حفظ کنید. با تنظیم TextToHtmlConversionOptions.setTextInheritanceLimit می‌توانید کنترل کنید که مبدل تا چه حد از وراثت فرمت‌بندی پیروی می‌کند.

the sample presentation را دانلود کنید و در پوشه کاری برنامه قرار دهید. اسلاید اول آن شامل یک شکل متنی با متن «Inherited text; explicit red 18pt» است. ورودی به‌طور عمدی از سطوح فرمت‌بندی زیر استفاده می‌کند:

  • سبک متن پیش‌فرض ارائه، Calibri با اندازه 28 pt را فراهم می‌کند.
  • سبک متن سطح اول چارچوب متنی، متن آبی و ایتالیک را فراهم می‌کند.
  • پاراگراف صراحتاً متن پررنگ را تنظیم می‌کند.
  • بخش دوم، «explicit red 18pt»، صراحتاً متن قرمز و اندازه 18 pt را تنظیم می‌کند.

مثال کامل زیر همان پاراگراف‌ها را سه بار صادر می‌کند. TextToHtmlConversionOptions را به متد صادرات پاس می‌دهد و هر نتیجه را به عنوان یک فایل HTML جداگانه UTF‑8 می‌نویسد:

import jpype
import asposeslides
from pathlib import Path

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import AutoShape, Presentation, TextInheritanceLimit, TextToHtmlConversionOptions

presentation = Presentation("text-inheritance.pptx")
try:
    shape = presentation.getSlides().get_Item(0).getShapes().get_Item(0)

    if isinstance(shape, AutoShape):
        text_frame = shape.getTextFrame()
        if text_frame is not None:
            paragraphs = text_frame.getParagraphs()
            
            modes = [TextInheritanceLimit.All, TextInheritanceLimit.TextBox, TextInheritanceLimit.ParagraphOnly]
            file_names = ["All.html", "TextBox.html", "ParagraphOnly.html"]

            for mode, file_name in zip(modes, file_names):
                options = TextToHtmlConversionOptions()
                options.setTextInheritanceLimit(mode)

                html = paragraphs.exportToHtml(0, paragraphs.getCount(), options)
                try:
                    Path(file_name).write_text(str(html), encoding="utf-8")
                except OSError as exception:
                    print("The HTML file could not be written: " + str(exception))
        else:
            print("The first shape does not contain a text frame.")
    else:
        print("The first shape is not a text shape.")
finally:
    presentation.dispose()

HTML تولید شده CSS زیر را برای اولین بخش «Inherited text; » دارد. جدول مربوطه اعلان‌های CSS مرتبط را نشان می‌دهد؛ دیگر ویژگی‌های تولید شده برای وضوح حذف شده‌اند.

Output Relevant CSS for the first portion Appearance
All.html font-family:'Calibri'; font-size:28pt; color:#0000ff; font-style:italic; font-weight:bold متن آبی، پررنگ، ایتالیک با اندازه 28 pt در Calibri، اگر آن فونت در دسترس باشد.
TextBox.html color:#0000ff; font-style:italic; font-weight:bold متن آبی، پررنگ، ایتالیک؛ صفحه یا مرورگر حاکم فونت و اندازه را فراهم می‌کند.
ParagraphOnly.html font-weight:bold متن پررنگ؛ صفحه یا مرورگر حاکم فونت، اندازه، رنگ و سبک ایتالیک را فراهم می‌کند.

بخش دوم color:#ff0000، font-size:18pt و font-weight:bold را در هر سه خروجی حفظ می‌کند. همچنین ایتالیک را در خروجی‌های All و TextBox حفظ می‌کند اما در ParagraphOnly نیست. محدود کردن وراثت بنابراین فرمت‌بندی صریحاً به یک پاراگراف یا بخش اختصاص داده شده را حذف نمی‌کند.

فایل‌ها را در همان مرورگر باز کنید تا آنها را مقایسه کنید. در دو حالت محدود، ویژگی‌های CSS حذف‌شده توسط سبک‌های صفحه مقصد یا پیش‌فرض‌های مرورگر حل می‌شوند، بنابراین ظاهر نهایی به مکانی که HTML استفاده می‌شود بستگی دارد.

تولید یک تکه کلپ‌بورد HTML

از TextToHtmlConversionOptions.setAddClipboardFragmentHeader برای انتخاب قالب خروجی ParagraphCollection.exportToHtml استفاده کنید. با False متد HTML عادی برمی‌گرداند. با True سرآیند کلپ‌بورد ویندوز را پیشوند می‌کند. فیلدهای StartHTML و EndHTML آفلست بایت‌های UTF‑8 HTML را می‌دهند. فیلدهای StartFragment و EndFragment محتوای قابل چسباندن را مشخص می‌کنند.

این مثال دو پاراگراف فرمت‌بندی‌شده را به عنوان HTML عادی (html) و به عنوان یک تکه کلپ‌بورد HTML (clipboard_html) صادر می‌کند. سپس برنامه می‌تواند این تکه را به کلیپ‌بورد سیستم بنویسد. اگر API کلیپ‌بورد انتظار بایت‌های خام داشته باشد، از clipboard_html.encode("utf-8") برای رمزگذاری تکه بدون علامت ترتیب بایت (BOM) استفاده کنید.

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, ShapeType, TextToHtmlConversionOptions

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)
    
    shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 0, 0, 400, 100)
    paragraphs = shape.getTextFrame().getParagraphs()
    paragraphs.clear()
    paragraphs.addFromHtml("<p>Status: <b>Ready</b></p><p><i>Next steps</i></p>")

    options = TextToHtmlConversionOptions()
    options.setAddClipboardFragmentHeader(False)
    html = str(paragraphs.exportToHtml(0, paragraphs.getCount(), options))

    options.setAddClipboardFragmentHeader(True)
    clipboard_html = str(paragraphs.exportToHtml(0, paragraphs.getCount(), options))
finally:
    presentation.dispose()

رندر یک پاراگراف به عنوان تصویر

Paragraph.getImage پاراگراف منفرد را مستقیم رندر می‌کند و شی تصویر را برمی‌گرداند. نتیجه را با متد save به فایل یا جریان ذخیره کنید. نیازی به رندر شکل حاوی آن یا برش دستی بیت‌مپ ندارید.

Paragraph.getImage می‌تواند None برگرداند اگر پاراگراف در مجموعه والد یافت نشود، محدوده رندر معتبری نداشته باشد یا نتواند رندر شود. قبل از ذخیره نتیجه را بررسی کنید و پس از استفاده تصویر برگردانده‌شده را آزاد کنید.

رندر پاراگراف با مقیاس پیش‌فرض

این مثال یک ارائه با یک اسلاید دارد که اولین شکل آن یک کادر متن شامل سه پاراگراف است.

The text box with three paragraphs

مثال زیر پاراگراف دوم را در اندیس 1 انتخاب می‌کند و بدون پارامترهای مقیاس به Paragraph.getImage فراخوانی می‌کند. بررسی می‌کند که شکل پاراگراف مورد نیاز را داراست و رندر تصویر را قبل از فراخوانی متد save برای ذخیره به فرمت PNG بررسی می‌کند. بلوک finally پس از استفاده تصویر را آزاد می‌کند.

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import AutoShape, ImageFormat, Presentation

presentation = Presentation("sample.pptx")
try:
    shape = presentation.getSlides().get_Item(0).getShapes().get_Item(0)

    if isinstance(shape, AutoShape):
        text_shape = shape
        text_frame = text_shape.getTextFrame()
        if text_frame is not None and text_frame.getParagraphs().getCount() > 1:
            paragraph = text_frame.getParagraphs().get_Item(1)
            paragraph_image = paragraph.getImage()

            if paragraph_image is not None:
                try:
                    paragraph_image.save("paragraph.png", ImageFormat.Png)
                finally:
                    paragraph_image.dispose()
            else:
                print("The paragraph could not be rendered.")
        else:
            print("The expected paragraph was not found.")
    else:
        print("The first shape is not a text shape.")
finally:
    presentation.dispose()

نتیجه:

The paragraph image

رندر پاراگراف در سلول جدول با مقیاس

از بارگذاری Paragraph.getImage که پارامترهای scale_x و scale_y را می‌پذیرد استفاده کنید تا عوامل مقیاس افقی و عمودی را تنظیم کنید. مثال زیر یک جدول ایجاد می‌کند، پاراگراف را در اولین سلول آن با دو برابر عرض و ارتفاع پیش‌فرض رندر می‌کند و نتیجه را به عنوان تصویر PNG ذخیره می‌کند.

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import ImageFormat, Presentation

scale_x = 2.0
scale_y = 2.0
presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    table = slide.getShapes().addTable(50, 50, [300.0], [80.0])

    paragraph = table.get_Item(0, 0).getTextFrame().getParagraphs().get_Item(0)
    paragraph.setText("Text in a table cell")

    paragraph_image = paragraph.getImage(scale_x, scale_y)

    if paragraph_image is not None:
        try:
            paragraph_image.save("table_paragraph.png", ImageFormat.Png)
        finally:
            paragraph_image.dispose()
    else:
        print("The paragraph could not be rendered.")
finally:
    presentation.dispose()

عامل مقیاس 1 آن محور را در اندازه پیکسل پیش‌فرض نگه می‌دارد. برای مثال 2 برای هر دو عامل تصویری تولید می‌کند که عرض و ارتفاع آن تقریباً دو برابر ابعاد پیش‌فرض است و چهار برابر پیکسل دارد. عوامل بزرگ‌تر معمولاً متن تیزتر برای بزرگ‌نمایی یا خروجی با وضوح بالا تولید می‌کنند، اما حافظه و حجم فایل را نیز افزایش می‌دهند. عوامل زیر 1 تصاویر کوچکتر با جزئیات کمتر تولید می‌کنند. برای حفظ نسبت‌عرض‑ارتفاع پاراگراف از عوامل برابر استفاده کنید؛ عوامل افقی و عمودی متفاوت خروجی را به صورت مستقل کش می‌دهند.

رندر کل شکل با Shape.getImage همچنان زمانی مفید است که خروجی نیاز به شامل پر، حاشیه یا زمینه بصری شکل داشته باشد. برای تصویر فقط پاراگراف، از Paragraph.getImage استفاده کنید.

سوالات متداول

آیا می‌توانم بسته شدن خطوط داخل یک چارچوب متنی را به‌طور کامل غیرفعال کنم؟

بله. با تنظیم TextFrameFormat.setWrapText بسته شدن را غیرفعال کنید تا خطوط در لبه‌های چارچوب متنی شکسته نشوند.

چگونه می‌توانم مرزهای دقیق روی اسلاید یک پاراگراف خاص را به دست آورم؟

از Paragraph.getRect برای دریافت مستطیل محاطی پاراگراف استفاده کنید. Portion.getRect محدوده یک بخش منفرد را فراهم می‌کند.

کنترل هم‌ترازی پاراگراف (چپ، راست، مرکز یا توزیع) در کجا انجام می‌شود؟

ParagraphFormat.setAlignment یک تنظیم سطح پاراگراف است و بر کل پاراگراف اعمال می‌شود صرف‌نظر از فرمت‌بندی بخش‌های جداگانه.

برای هم‌ترازی عمودی بخش‌های با اندازه‌های فونت متفاوت در هر خط، به Align Fonts Within a Line مراجعه کنید.

آیا می‌توانم زبان اصلاحی را برای بخشی از یک پاراگراف تنظیم کنم؟

بله. با تنظیم BasePortionFormat.setLanguageId برای بخش‌های منفرد، یک پاراگراف می‌تواند متنی با چند زبان مختلف داشته باشد.