Com formatar text en Python

Com formatar text en Python

Aspose.Slides FOSS for Python provides fine-grained text formatting through the PortionFormat classe. A Portion és la unitat independent més petita de text; es correspon a una única execució de format dins d’un paràgraf. Aquesta guia mostra com aplicar format en negreta, cursiva, mida de lletra i color al text en una presentació.

Guia pas a pas

Pas 1: Instal·la el paquet

pip install aspose-slides-foss

Pas 2: Afegeix una forma amb un Text Frame

Abans de formatar el text, una forma ha de contenir una TextFrame. Utilitza shape.add_text_frame() per crear-ne una.

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)

Pas 3: Accedeix al TextFrame

shape.add_text_frame() retorna el TextFrame objecte. També podeu recuperar-lo més tard a través de shape.text_frame.

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

Un TextFrame conté una llista de Paragraph objectes (tf.paragraphs). Cada Paragraph conté Portion objectes (paragraph.portions).


Pas 4: Aplica format de negreta i cursiva

Utilitzeu portion_format.font_bold i portion_format.font_italic. Aquestes propietats accepten NullableBool.TRUE, NullableBool.FALSE, o NullableBool.NOT_DEFINED (heretat del mestre).

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)

Pas 5: Defineix la mida i el color de la font

Estableix portion_format.font_height per a la mida (en punts) i utilitzeu fill_format per al color.

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) accepta valors de 0–255 per a cada canal.


Pas 6: Múltiples portions en un paràgraf

Un únic paràgraf pot contenir diverses porcions amb formatatge diferent. Afegeix un nou Portion a la col·lecció d’un paràgraf portions col·lecció:

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)

Problemes comuns i solucions

El text apareix negre fins i tot després d’establir el color

Assegureu fill_format.fill_type = FillType.SOLID està establert abans d’assignar el color. Sense establir el tipus d’emplenament, el canvi de color pot no tenir cap efecte.

NullableBool.TRUE vs True

portion_format.font_bold espera NullableBool.TRUE, no el Python True. Assignant Python True pot generar una TypeError o no fer res silenciosament segons la vinculació.

La font no apareix al fitxer desat

El latin_font La propietat estableix la família de fonts llatines. Si no s’estableix, s’utilitza la font del tema de la presentació. Les fonts personalitzades han d’estar incrustades o disponibles a la màquina de visualització.


Preguntes freqüents

Com canvio la família de fonts?

Estableix portion_format.latin_font:

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

FontData accepta el nom de la família de fonts com a cadena.

Com estableixo l’alineació del paràgraf?

Utilitza paragraph_format.alignment:

from aspose.slides_foss import TextAlignment

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

Valors compatibles: LEFT, CENTER, RIGHT, JUSTIFY.

Com estableixo l’interlineat?

Utilitza paragraph_format.space_before (punts abans del paràgraf) o paragraph_format.space_after (punts després del paràgraf):

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

Vegeu també

 Català