مدیریت پاراگرافهای متن PowerPoint در Python از طریق Java
مرور کلی
Aspose.Slides برای Python از طریق Java متن را به عنوان یک سلسلهمراتب از فریمهای متنی، پاراگرافها و بخشها نمایش میدهد:
- TextFrame متن را درون یک شکل نگه میدارد و دسترسی به مجموعه پاراگرافهای آن را فراهم میکند.
- Paragraph یک پاراگراف در فریم متنی را نشان میدهد و دسترسی به بخشها و قالببندی سطح پاراگراف را فراهم میکند.
- Portion یک بخش متن داخل یک پاراگراف را نشان میدهد. هر بخش میتواند متن و قالببندی کاراکتری خاص خود را داشته باشد.
بنابراین یک پاراگراف میتواند متن با فونتها، رنگها، اندازهها و قالببندیهای مختلف را از طریق استفاده از چندین بخش داشته باشد.
ایجاد و قالببندی پاراگرافها
ایجاد پاراگرافها با چندین بخش
مراحل زیر یک فریم متنی با سه پاراگراف، هر یک شامل سه بخش، ایجاد میکند:
- یک نمونه از کلاس Presentation ایجاد کنید.
- اسلاید مربوطه را از طریق شاخص آن دسترسی پیدا کنید.
- یک AutoShape مستطیلی به اسلاید اضافه کنید.
- به TextFrame شکل دسترسی پیدا کنید.
- از پاراگراف پیشفرض استفاده کنید و دو شیء Paragraph دیگر به فریم متنی اضافه کنید.
- به اندازه کافی شیء Portion اضافه کنید تا هر پاراگراف شامل سه بخش شود. پاراگراف پیشفرض از قبل یک بخش خالی دارد.
- متن هر بخش را تنظیم کنید.
- قالببندی کاراکتری را از طریق Portion.getPortionFormat اعمال کنید.
- ارائه (presentation) اصلاحشده را ذخیره کنید.
این مثال پایتون مراحل فوق را پیادهسازی میکند:
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 تعریف میشود.
- یک نمونه از کلاس Presentation ایجاد کنید.
- اسلاید مربوطه را از طریق شاخص آن دسترسی پیدا کنید.
- یک AutoShape به اسلاید انتخابشده اضافه کنید.
- به TextFrame شکل دسترسی پیدا کنید.
- پاراگراف پیشفرض را از فریم متنی حذف کنید.
- یک Paragraph برای نقطه نمادیک (symbol bullet) ایجاد کنید.
- BulletFormat.setType را روی BulletType.Symbol تنظیم کنید و کاراکتر نقطه را مشخص کنید.
- متن پاراگراف، تورفتگی، رنگ نقطه و ارتفاع نقطه را تنظیم کنید.
- پاراگراف را به فریم متنی اضافه کنید.
- پاراگراف دوم را ایجاد کرده و BulletFormat.setType را روی BulletType.Numbered تنظیم کنید.
- سبک نقطه شمارهدار را پیکربندی کنید و پاراگراف را به فریم متنی اضافه کنید.
- ارائه را ذخیره کنید.
این مثال پایتون یک نقطه نمادیک و یک نقطه شمارهدار ایجاد میکند:
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()
استفاده از نقطههای تصویری
نقطههای تصویری اجازه میدهند به جای نماد یا عدد از تصویر دلخواه استفاده کنید.
- یک نمونه از کلاس Presentation ایجاد کنید.
- اسلاید مربوطه را از طریق شاخص آن دسترسی پیدا کنید.
- یک AutoShape اضافه کنید و به TextFrame آن دسترسی پیدا کنید.
- پاراگراف پیشفرض را از فریم متنی حذف کنید.
- تصویر نقطه را بارگذاری کنید و به مجموعه تصویرهای ارائه به عنوان یک PPImage اضافه کنید.
- یک Paragraph ایجاد کرده و متن آن را تنظیم کنید.
- BulletFormat.setType را روی BulletType.Picture تنظیم کنید.
- تصویر را از طریق BulletFormat.getPicture اختصاص دهید و ارتفاع نقطه را تنظیم کنید.
- پاراگراف را به فریم متنی اضافه کنید.
- ارائه اصلاحشده را ذخیره کنید.
این مثال پایتون یک نقطه تصویری ایجاد میکند:
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 دارد.
- یک Presentation ایجاد کنید و یک اسلاید را دسترسی پیدا کنید.
- یک AutoShape اضافه کنید و پاراگراف پیشفرض را از فریم متنی آن پاک کنید.
- چهار پاراگراف ایجاد کنید و نمادهای نقطه آنها را پیکربندی کنید.
- مقادیر ParagraphFormat.setDepth آنها را به ترتیب
0،1،2و3تنظیم کنید. - پاراگرافها را به فریم متنی اضافه کنید و ارائه را ذخیره کنید.
این مثال پایتون یک فهرست نقطهای چهار سطحی ایجاد میکند:
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 برای تنظیم عدد اولیه نمایشدادهشده برای پاراگراف شمارهدار استفاده کنید.
- یک Presentation ایجاد کنید و یک AutoShape را به اسلاید اضافه کنید.
- پاراگراف پیشفرض را از فریم متنی شکل پاک کنید.
- سه پاراگراف شمارهدار ایجاد کنید.
- برای هر پاراگراف، 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.setIndent را برای نشان دادن تأثیر تورفتگی خط اول بر چیدمان پاراگراف اعمال میکند.
- یک نمونه از کلاس Presentation ایجاد کنید.
- اسلاید هدف را دسترسی پیدا کنید.
- یک AutoShape مستطیلی به اسلاید اضافه کنید.
- به TextFrame شکل دسترسی پیدا کنید و پاراگراف پیشفرض را حذف کنید.
- چند پاراگراف ایجاد کنید و مقادیر مختلف ParagraphFormat.setIndent را برای آنها تنظیم کنید.
- پاراگرافها را به فریم متنی اضافه کنید.
- ارائه اصلاحشده را ذخیره کنید.
این کد نشان میدهد چگونه تورفتگی پاراگراف را تنظیم کنید:
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().getFillFormat().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().getFillFormat().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()
نتیجه:

تنظیم تورفتگی مشبک (Hanging Indent)
تورفتگی مشبک نوعی چیدمان پاراگراف است که در آن خط اول به سمت چپ خطوط باقیمانده میآید. در Aspose.Slides این اثر را با ParagraphFormat.setIndent ایجاد میکنید. مقدار منفی به خط اول اجازه میدهد نسبت به بدن پاراگراف به سمت چپ حرکت کند.
در عمل، ParagraphFormat.setMarginLeft موقعیت چپ بدن پاراگراف را تعریف میکند و ParagraphFormat.setIndent موقعیت خط اول را نسبت به آن حاشیه تعیین میکند. برای ایجاد تورفتگی مشبک، مقدار مثبت به ParagraphFormat.setMarginLeft بدهید و مقدار منفی به ParagraphFormat.setIndent بدهید.
این قالببندی برای کتابشناسیها، مراجع، واژهنامهها و سایر پاراگرافهایی که خطوط بستهشده باید زیر بدن پاراگراف همراستا شوند مفید است.
- یک نمونه از کلاس Presentation ایجاد کنید.
- اسلاید هدف را دسترسی پیدا کنید.
- یک AutoShape مستطیلی به اسلاید اضافه کنید.
- به TextFrame شکل دسترسی پیدا کنید و پاراگراف پیشفرض را حذف کنید.
- برای هر پاراگراف مقدار مثبت به ParagraphFormat.setMarginLeft بدهید.
- مقدار منفی به ParagraphFormat.setIndent بدهید تا اثر تورفتگی مشبک ایجاد شود.
- پاراگرافها را به فریم متنی اضافه کنید.
- ارائه اصلاحشده را ذخیره کنید.
این کد نشان میدهد چگونه تورفتگی مشبک را برای یک پاراگراف تنظیم کنید:
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()
نتیجه:

تنظیم ویژگیهای انتهای پاراگراف (End Paragraph Run Properties)
Paragraph.setEndParagraphPortionFormat قالببندی علامت پایان پاراگراف را کنترل میکند. مثال زیر اندازه قلم و قلم لاتین را به علامت پایان پاراگراف دوم اختصاص میدهد:
- یک Presentation بارگیری کنید و به یک اسلاید دسترسی پیدا کنید.
- یک AutoShape اضافه کنید و پاراگراف پیشفرض آن را پاک کنید.
- دو پاراگراف ایجاد کنید و به آنها بخشهای متنی اضافه کنید.
- یک PortionFormat برای علامت پایان پاراگراف دوم ایجاد کنید.
- BasePortionFormat.setFontHeight و BasePortionFormat.setLatinFont را تنظیم کنید.
- قالب را با Paragraph.setEndParagraphPortionFormat اختصاص دهید و ارائه را ذخیره کنید.
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()
واردات و صادرات محتوای پاراگراف
وارد کردن متن HTML به پاراگرافها
از ParagraphCollection.addFromHtml برای تبدیل نشانهگذاری HTML به پاراگرافها و بخشها در یک فریم متنی استفاده کنید.
- یک نمونه از کلاس Presentation ایجاد کنید.
- یک اسلاید را دسترسی پیدا کنید و یک AutoShape اضافه کنید.
- به TextFrame شکل دسترسی پیدا کنید و پاراگراف پیشفرض را حذف کنید.
- فایل HTML منبع را بخوانید.
- رشته HTML را به ParagraphCollection.addFromHtml پاس دهید.
- ارائه اصلاحشده را ذخیره کنید.
این مثال پایتون HTML را به یک فریم متنی وارد میکند:
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 استفاده کنید.
- یک نمونه از کلاس Presentation ایجاد کنید و ارائه مورد نظر را بارگیری کنید.
- اسلاید را دسترسی پیدا کنید و AutoShape حاوی متن را پیدا کنید.
- به TextFrame شکل دسترسی پیدا کنید.
- با استفاده از ParagraphCollection.exportToHtml اندیس پاراگراف شروع و تعداد پاراگرافهای مورد نظر برای صادرات را مشخص کنید.
- رشته HTML برگشتی را در یک فایل بنویسید.
این مثال پایتون تمام پاراگرافها را از اولین شکل متنی صادر میکند:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import AutoShape, Presentation
from pathlib import Path
presentation = Presentation("ExportingHTMLText.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:
paragraphs = text_frame.getParagraphs()
html = paragraphs.exportToHtml(0, paragraphs.getCount(), None)
try:
Path("paragraphs.html").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()
رندر کردن یک پاراگراف به عنوان تصویر
Paragraph.getImage یک پاراگراف منفرد را مستقیماً رندر میکند و شیء تصویر را بازمیگرداند. نتیجه را با متد save در یک فایل یا جریان ذخیره کنید. نیازی به رندر شکل حاوی آن یا برش دستی bitmap نیست.
Paragraph.getImage میتواند None برگرداند اگر پاراگراف در مجموعه والد پیدا نشود، مرزهای رندر معتبر نداشته باشد یا نتواند رندر شود. قبل از ذخیره نتیجه را بررسی کنید و پس از استفاده تصویر برگشتی را آزاد کنید.
رندر کردن یک پاراگراف با مقیاس پیشفرض
فرض کنید فایلی به نام sample.pptx داریم که یک اسلاید دارد و اولین شکل آن یک جعبه متنی شامل سه پاراگراف است.

مثال زیر پاراگراف دوم را در یک شکل متنی عادی با مقیاس پیشفرض رندر میکند و تصویر برگشتی را در قالب 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()
نتیجه:

رندر کردن یک پاراگراف در یک سلول جدول با مقیاسدهی
از overload 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 تنظیم سطح پاراگراف است و بر تمام پاراگراف اعمال میشود، صرفنظر از قالببندی هر بخش.
آیا میتوانم زبان proofing را برای بخشی از یک پاراگراف تنظیم کنم؟
بله. برای بخشهای منفرد BasePortionFormat.setLanguageId را تنظیم کنید تا یک پاراگراف بتواند متنهای چند زبانی داشته باشد.