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 provides fine-grained text formatting through the PortionFormat lớp. A Portion là đơn vị độc lập nhỏ nhất của văn bản; nó tương ứng với một chuỗi đị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 một 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 Shape với Text Frame

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

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ề TextFrame đối tượng. Bạn cũng có thể lấy lại nó sau này 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 Paragraph đối tượng (tf.paragraphs). Mỗi Paragraph chứa Portion đối tượng (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 Sắc Phông Chữ

Đặt portion_format.font_height cho kích thước (theo đ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 Portion trong Một Đoạn Văn

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

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 vẫn hiển thị màu đen ngay cả sau 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ô, 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 ràng buộc.

Phông chữ không xuất hiện trong tệp đã lưu

Thuộc tính latin_font thuộc tính đặt họ phông chữ Latin. Nếu không được đặt, phông chữ chủ đề trình chiếu sẽ được sử dụng. 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 sao tôi có thể thay đổi họ phông chữ?

Đặt 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 sao tôi có thể đặ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 sao tôi có thể đặ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