Comment formater le texte dans Python
Aspose.Slides FOSS for Python provides fine-grained text formatting through the PortionFormat classe. A Portion est la plus petite unité indépendante de texte ; elle correspond à une seule séquence de formatage au sein d’un paragraphe. Ce guide montre comment appliquer le gras, l’italique, la taille de police et le format de couleur au texte dans une présentation.
Guide étape par étape
Étape 1 : Installer le package
pip install aspose-slides-fossÉtape 2 : Ajouter une forme avec un cadre de texte
Avant de formater le texte, une forme doit contenir un TextFrame. Utilisez shape.add_text_frame() pour en créer une.
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)Étape 3 : Accéder au TextFrame
shape.add_text_frame() renvoie le TextFrame objet. Vous pouvez également le récupérer plus tard via shape.text_frame.
tf = shape.text_frame # if the frame already exists
tf = shape.add_text_frame("") # creates a new frameUn TextFrame contient une liste de Paragraph objets (tf.paragraphs). Chaque Paragraph contient Portion objets (paragraph.portions).
Étape 4 : Appliquer le formatage gras et italique
Utilisez portion_format.font_bold et portion_format.font_italic. Ces propriétés acceptent NullableBool.TRUE, NullableBool.FALSE, ou NullableBool.NOT_DEFINED (hérite du 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)Étape 5 : Définir la taille et la couleur de la police
Définir portion_format.font_height pour la taille (en points) et utilisez fill_format pour la couleur.
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) accepte des valeurs 0–255 pour chaque canal.
Étape 6 : Plusieurs portions dans un même paragraphe
Un seul paragraphe peut contenir plusieurs parties avec un formatage différent. Ajoutez un nouveau Portion à la collection d’un paragraphe portions collection:
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)Problèmes courants et solutions
Le texte apparaît en noir même après avoir défini la couleur
Assurez‑vous fill_format.fill_type = FillType.SOLID est défini avant d’attribuer la couleur. Sans définir le type de remplissage, le changement de couleur peut n’avoir aucun effet.
NullableBool.TRUE vs True
portion_format.font_bold s’attend à NullableBool.TRUE, pas le Python True. Attribuer Python True peut déclencher un TypeError ou ne rien faire silencieusement selon le binding.
La police n’apparaît pas dans le fichier enregistré
Le latin_font la propriété définit la famille de polices Latin. Si elle n’est pas définie, la police du thème de présentation est utilisée. Les polices personnalisées doivent être incorporées ou disponibles sur la machine de visualisation.
Foire aux questions
Comment changer la famille de polices ?
Définir portion_format.latin_font:
fmt.latin_font = slides.FontData("Arial")FontData accepte le nom de la famille de polices sous forme de chaîne.
Comment définir l’alignement du paragraphe ?
Utiliser paragraph_format.alignment:
from aspose.slides_foss import TextAlignment
tf.paragraphs[0].paragraph_format.alignment = TextAlignment.CENTERValeurs prises en charge : LEFT, CENTER, RIGHT, JUSTIFY.
Comment définir l’espacement des lignes ?
Utiliser paragraph_format.space_before (points avant le paragraphe) ou paragraph_format.space_after (points après le paragraphe) :
tf.paragraphs[0].paragraph_format.space_before = 12 # 12pt before
tf.paragraphs[0].paragraph_format.space_after = 6 # 6pt after