วิธีจัดรูปแบบข้อความใน Python
Aspose.Slides FOSS for Python provides fine-grained text formatting through the PortionFormat class. A Portion เป็นหน่วยข้อความอิสระที่เล็กที่สุด; มันสอดคล้องกับการจัดรูปแบบครั้งเดียวภายในย่อหน้า คู่มือฉบับนี้แสดงวิธีการใช้การจัดรูปแบบตัวหนา, ตัวเอียง, ขนาดฟอนต์, และสีกับข้อความในงานนำเสนอ.
คู่มือแบบขั้นตอนต่อขั้นตอน
ขั้นตอนที่ 1: ติดตั้งแพ็กเกจ
pip install aspose-slides-fossStep 2: Add a Shape with a Text Frame
ก่อนทำการจัดรูปแบบข้อความ, shape ต้องมี a 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)Step 3: Access the TextFrame
shape.add_text_frame() คืนค่า TextFrame object. คุณสามารถดึงคืนได้ในภายหลังผ่าน shape.text_frame.
tf = shape.text_frame # if the frame already exists
tf = shape.add_text_frame("") # creates a new frameA TextFrame มีรายการของ Paragraph objects (tf.paragraphs). แต่ละ Paragraph มี Portion objects (paragraph.portions).
Step 4: Apply Bold and Italic Formatting
ใช้ 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)Step 5: Set Font Size and Color
ตั้งค่า 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) รับค่าตั้งแต่ 0–255 สำหรับแต่ละช่อง.
Step 6: Multiple Portions in One Paragraph
ย่อหน้าเดียวสามารถมีหลายส่วนที่มีการจัดรูปแบบต่างกันได้ เพิ่มใหม่ 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)ปัญหาทั่วไปและวิธีแก้
Text appears black even after setting color
ตรวจสอบให้แน่ใจว่า fill_format.fill_type = FillType.SOLID is set ก่อนกำหนดสี. หากไม่ได้ตั้งค่า fill type การเปลี่ยนสีอาจไม่มีผล.
NullableBool.TRUE vs True
portion_format.font_bold คาดหวัง NullableBool.TRUE, ไม่ใช่ Python True. การกำหนดค่า Python True อาจทำให้เกิด TypeError หรือทำอะไรไม่ได้โดยเงียบตามการผูกโยง.
แบบอักษรไม่ปรากฏในไฟล์ที่บันทึก
ค่า latin_font property กำหนดฟอนต์ Latin. หากไม่ได้ตั้งค่า จะใช้ฟอนต์ของธีมการนำเสนอ ฟอนต์ที่กำหนดเองต้องฝังไว้หรือมีบนเครื่องที่ดู.
คำถามที่พบบ่อย
ฉันจะเปลี่ยนตระกูลแบบอักษรได้อย่างไร?
ตั้งค่า 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