Cách Định Dạng Văn Bản trong Python

Cách Định Dạng Văn Bản trong Python

Aspose.Slides FOSS for Python cung cấp khả năng định dạng văn bản chi tiết thông qua lớp PortionFormat. Một Portion là đơn vị độc lập nhỏ nhất của văn bản; nó tương ứng với một đoạn định dạng duy nhất trong một đoạn văn. Hướng dẫn này cho thấy cách áp dụng định dạng in đậm, in nghiêng, kích thước phông chữ và màu sắc cho văn bản trong bản trình chiếu.

Hướng Dẫn Từng Bước

Bước 1: Cài đặt gói

pip install aspose-slides-foss

Bước 2: Thêm một hình dạng với khung văn bản

Trước khi định dạng văn bản, một hình dạng phải chứa TextFrame. Sử dụng shape.add_text_frame() để tạo một.

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)

Bước 3: Truy cập TextFrame

shape.add_text_frame() trả về đối tượng TextFrame. Bạn cũng có thể lấy lại nó sau này thông qua shape.text_frame.

tf = shape.text_frame          # if the frame already exists
tf = shape.add_text_frame("") # creates a new frame

Một TextFrame chứa một danh sách các đối tượng Paragraph (tf.paragraphs). Mỗi Paragraph chứa các đối tượng Portion (paragraph.portions).


Bước 4: Áp dụng định dạng in đậm và in nghiêng

Sử dụng portion_format.font_boldportion_format.font_italic. Các thuộc tính này chấp nhận NullableBool.TRUE, NullableBool.FALSE hoặc NullableBool.NOT_DEFINED (kế thừa từ 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)

Bước 5: Đặt kích thước và màu chữ

Đặt portion_format.font_height cho kích thước (đơn vị điểm) và sử dụng fill_format cho màu.

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) chấp nhận các giá trị 0–255 cho mỗi kênh.


Bước 6: Nhiều phần trong một đoạn

Một đoạn văn có thể chứa nhiều phần với định dạng khác nhau. Thêm một Portion mới vào bộ sưu tập portions của đoạn văn:

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)

Các vấn đề thường gặp và cách khắc phục

Văn bản hiển thị màu đen ngay cả khi đã đặt màu

Đảm bảo fill_format.fill_type = FillType.SOLID được đặt trước khi gán màu. Nếu không đặt loại tô màu, việc thay đổi màu có thể không có hiệu lực.

NullableBool.TRUE vs True

portion_format.font_bold mong đợi NullableBool.TRUE, không phải Python True. Gán Python True có thể gây ra một TypeError hoặc im lặng không làm gì tùy thuộc vào binding.

Phông chữ không hiển thị trong tệp đã lưu

Thuộc tính latin_font thiết lập họ phông chữ Latin. Nếu không được thiết lập, phông chữ của giao diện trình chiếu sẽ được sử dụng. Các phông chữ tùy chỉnh phải được nhúng hoặc có sẵn trên máy xem.


Câu hỏi thường gặp

Làm thế nào để thay đổi họ phông chữ?

Thiết lập portion_format.latin_font:

fmt.latin_font = slides.FontData("Arial")

FontData chấp nhận tên họ phông chữ dưới dạng chuỗi.

Làm thế nào để đặt căn chỉnh đoạn văn?

Sử dụng paragraph_format.alignment:

from aspose.slides_foss import TextAlignment

tf.paragraphs[0].paragraph_format.alignment = TextAlignment.CENTER

Các giá trị được hỗ trợ: LEFT, CENTER, RIGHT, JUSTIFY.

Làm thế nào để tôi đặt khoảng cách dòng?

Sử dụng paragraph_format.space_before (điểm trước đoạn) hoặc paragraph_format.space_after (điểm sau đoạn):

tf.paragraphs[0].paragraph_format.space_before = 12   # 12pt before
tf.paragraphs[0].paragraph_format.space_after = 6     # 6pt after

Xem thêm

 Tiếng Việt