วิธีจัดรูปแบบข้อความใน Python

วิธีจัดรูปแบบข้อความใน Python

Aspose.Slides FOSS for Python ให้การจัดรูปแบบข้อความแบบละเอียดผ่านคลาส PortionFormat. Portion คือหน่วยข้อความอิสระที่เล็กที่สุด; มันแมปไปยังการรันการจัดรูปแบบเดียวภายในย่อหน้า. คู่มือนี้แสดงวิธีการใช้การจัดรูปแบบตัวหนา, ตัวเอียง, ขนาดฟอนต์, และสีกับข้อความในงานนำเสนอ.

คู่มือแบบขั้นตอนต่อขั้นตอน

ขั้นตอนที่ 1: ติดตั้งแพคเกจ

pip install aspose-slides-foss

ขั้นตอนที่ 2: เพิ่มรูปทรงพร้อมกรอบข้อความ

ก่อนจัดรูปแบบข้อความ รูปร่างต้องมี 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)

ขั้นตอนที่ 3: เข้าถึง 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 frame

TextFrame มีรายการของวัตถุ Paragraph (tf.paragraphs). แต่ละ Paragraph ประกอบด้วยวัตถุ Portion (paragraph.portions).


ขั้นตอนที่ 4: ใช้การจัดรูปแบบตัวหนาและตัวเอียง

ใช้ 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)

ขั้นตอนที่ 5: ตั้งค่าขนาดและสีของฟอนต์

ตั้งค่า 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 สำหรับแต่ละช่องสัญญาณ.


ขั้นตอนที่ 6: หลายส่วนในย่อหน้าเดียว

ย่อหน้าเดียวสามารถมีหลายส่วนที่มีการจัดรูปแบบต่างกัน เพิ่ม 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 กำหนดฟอนต์ตระกูล 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

ดูเพิ่มเติม

 ภาษาไทย