نحوه قالببندی متن در Python
Aspose.Slides FOSS for Python provides fine-grained text formatting through the PortionFormat کلاس. A Portion کوچکترین واحد مستقل متن است؛ به یک اجرای قالببندی تک در یک پاراگراف نگاشت میشود. این راهنما نشان میدهد چگونه قالببندی بولد، ایتالیک، اندازه قلم و رنگ را بر روی متن در یک ارائه اعمال کنید.
راهنمای گام به گام
مرحله ۱: نصب بسته
pip install aspose-slides-fossمرحله ۲: افزودن یک شکل با یک قاب متن
قبل از قالببندی متن، یک شکل باید شامل یک TextFrame. استفاده کنید shape.add_text_frame() برای ایجاد یک مورد.
import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType
from aspose.slides_foss.export import SaveFormat
with slides.Presentation() as prs:
slide = prs.slides[0]
shape = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 50, 500, 150)
tf = shape.add_text_frame("Default text: will be formatted")
prs.save("output.pptx", SaveFormat.PPTX)مرحله ۳: دسترسی به TextFrame
shape.add_text_frame() باز میگرداند TextFrame شی. همچنین میتوانید بعداً آن را از طریق shape.text_frame.
tf = shape.text_frame # if the frame already exists
tf = shape.add_text_frame("") # creates a new frameA TextFrame شامل یک فهرست از Paragraph اشیاء (tf.paragraphs). هر Paragraph شامل Portion اشیاء (paragraph.portions).
مرحله ۴: اعمال قالببندی ضخیم و ایتالیک
استفاده کنید portion_format.font_bold و portion_format.font_italic. این ویژگیها میپذیرند NullableBool.TRUE, NullableBool.FALSE,، یا NullableBool.NOT_DEFINED (از master ارثبری میشود).
import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType, NullableBool
from aspose.slides_foss.export import SaveFormat
with slides.Presentation() as prs:
slide = prs.slides[0]
shape = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 50, 500, 150)
tf = shape.add_text_frame("Bold and italic text")
fmt = tf.paragraphs[0].portions[0].portion_format
fmt.font_bold = NullableBool.TRUE
fmt.font_italic = NullableBool.TRUE
prs.save("bold-italic.pptx", SaveFormat.PPTX)مرحله ۵: تنظیم اندازه قلم و رنگ
تنظیم portion_format.font_height برای اندازه (به نقطه) و استفاده fill_format برای رنگ.
import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType, NullableBool, FillType
from aspose.slides_foss.drawing import Color
from aspose.slides_foss.export import SaveFormat
with slides.Presentation() as prs:
slide = prs.slides[0]
shape = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 50, 500, 150)
tf = shape.add_text_frame("Large corporate-blue heading")
fmt = tf.paragraphs[0].portions[0].portion_format
fmt.font_height = 32 # 32pt font
fmt.font_bold = NullableBool.TRUE
fmt.fill_format.fill_type = FillType.SOLID
fmt.fill_format.solid_fill_color.color = Color.from_argb(255, 0, 70, 127)
prs.save("colored-text.pptx", SaveFormat.PPTX)Color.from_argb(alpha, red, green, blue) مقادیر ۰–۲۵۵ را برای هر کانال میپذیرد.
مرحله ۶: چندین Portion در یک پاراگراف
یک پاراگراف میتواند شامل چندین بخش با قالببندی متفاوت باشد. یک مورد جدید Portion به پاراگراف portions مجموعه:
import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType, NullableBool, FillType
from aspose.slides_foss.drawing import Color
from aspose.slides_foss.export import SaveFormat
with slides.Presentation() as prs:
slide = prs.slides[0]
shape = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 50, 600, 100)
tf = shape.add_text_frame("") # start with empty frame
paragraph = tf.paragraphs[0]
# First portion: normal text
portion1 = paragraph.portions[0]
portion1.text = "Normal text followed by "
portion1.portion_format.font_height = 20
# Second portion: bold red text
portion2 = slides.Portion()
portion2.text = "bold red text"
portion2.portion_format.font_height = 20
portion2.portion_format.font_bold = NullableBool.TRUE
portion2.portion_format.fill_format.fill_type = FillType.SOLID
portion2.portion_format.fill_format.solid_fill_color.color = Color.from_argb(255, 200, 0, 0)
paragraph.portions.add(portion2)
prs.save("mixed-format.pptx", SaveFormat.PPTX)مشکلات رایج و راهحلها
متن حتی پس از تنظیم رنگ، سیاه نمایش داده میشود
اطمینان حاصل کنید fill_format.fill_type = FillType.SOLID قبل از اختصاص رنگ تنظیم شده باشد. بدون تنظیم نوع پر کردن، ممکن است تغییر رنگ اثر نداشته باشد.
NullableBool.TRUE در مقابل True
portion_format.font_bold انتظار دارد NullableBool.TRUE,، نه Python True. اختصاص Python True ممکن است یک TypeError یا بهصورت ساکت هیچ کاری انجام ندهد بسته به بایندینگ.
قلم در فایل ذخیرهشده ظاهر نمیشود
این latin_font ویژگی خانوادهٔ فونت لاتین را تنظیم میکند. اگر تنظیم نشود، فونت تم ارائه استفاده میشود. فونتهای سفارشی باید جاسازی شوند یا بر روی دستگاه مشاهدهکننده در دسترس باشند.
سوالات متداول
چگونه میتوانم خانوادهٔ قلم را تغییر دهم؟?
تنظیم portion_format.latin_font:
fmt.latin_font = slides.FontData("Arial")FontData نام خانواده قلم را بهصورت رشته میپذیرد.
چگونه میتوانم تراز پاراگراف را تنظیم کنم؟?
استفاده کنید paragraph_format.alignment:
from aspose.slides_foss import TextAlignment
tf.paragraphs[0].paragraph_format.alignment = TextAlignment.CENTERمقادیر پشتیبانیشده: LEFT, CENTER, RIGHT, JUSTIFY.
چگونه میتوانم فاصلهٔ خطوط را تنظیم کنم؟?
استفاده کنید paragraph_format.space_before (نقطهها قبل از پاراگراف) یا paragraph_format.space_after (نقطهها بعد از پاراگراف):
tf.paragraphs[0].paragraph_format.space_before = 12 # 12pt before
tf.paragraphs[0].paragraph_format.space_after = 6 # 6pt after