Wie man Text in Python formatiert
Aspose.Slides FOSS for Python provides fine-grained text formatting through the PortionFormat Klasse. A Portion ist die kleinste unabhängige Texteinheit; sie entspricht einem einzelnen Formatierungslauf innerhalb eines Absatzes. Dieser Leitfaden zeigt, wie man Fett, Kursiv, Schriftgröße und Farbformatierung auf Text in einer Präsentation anwendet.
Schritt-für-Schritt-Anleitung
Schritt 1: Paket installieren
pip install aspose-slides-fossSchritt 2: Eine Form mit einem Textfeld hinzufügen
Bevor Text formatiert wird, muss eine Form ein TextFrame. Verwenden Sie shape.add_text_frame() um eines zu erstellen.
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)Schritt 3: Auf das TextFrame zugreifen
shape.add_text_frame() gibt das TextFrame Objekt zurück. Sie können es später auch über shape.text_frame.
tf = shape.text_frame # if the frame already exists
tf = shape.add_text_frame("") # creates a new frameEin TextFrame enthält eine Liste von Paragraph Objekten (tf.paragraphs). Jeder Paragraph enthält Portion Objekte (paragraph.portions).
Schritt 4: Fett‑ und Kursivformatierung anwenden
Verwenden portion_format.font_bold und portion_format.font_italic. Diese Eigenschaften akzeptieren NullableBool.TRUE, NullableBool.FALSE, oder NullableBool.NOT_DEFINED (von Master erben).
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)Schritt 5: Schriftgröße und Farbe festlegen
Setzen portion_format.font_height für die Größe (in Punkten) und verwenden fill_format für die Farbe.
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) akzeptiert Werte von 0–255 für jeden Kanal.
Schritt 6: Mehrere Portionen in einem Absatz
Ein einzelner Absatz kann mehrere Abschnitte mit unterschiedlicher Formatierung enthalten. Füge ein neues Portion zu einem Absatz portions Sammlung:
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)Häufige Probleme und Lösungen
Text erscheint schwarz, selbst nach dem Festlegen der Farbe
Stelle sicher fill_format.fill_type = FillType.SOLID ist festgelegt, bevor die Farbe zugewiesen wird. Ohne das Festlegen des Fülltyps kann die Farbänderung keine Wirkung haben.
NullableBool.TRUE vs True
portion_format.font_bold erwartet NullableBool.TRUE, nicht das Python True. Zuweisen von Python True kann einen TypeError oder stillschweigend nichts tun, abhängig von der Bindung.
Schriftart erscheint nicht in der gespeicherten Datei
Die latin_font Die Eigenschaft legt die lateinische Schriftfamilie fest. Wenn sie nicht festgelegt ist, wird die Schriftart des Präsentationsthemas verwendet. Benutzerdefinierte Schriftarten müssen eingebettet oder auf dem Anzeigegerät verfügbar sein.
Häufig gestellte Fragen
Wie ändere ich die Schriftfamilie?
Setzen portion_format.latin_font:
fmt.latin_font = slides.FontData("Arial")FontData akzeptiert den Namen der Schriftfamilie als Zeichenkette.
Wie stelle ich die Absatzausrichtung ein?
Verwenden paragraph_format.alignment:
from aspose.slides_foss import TextAlignment
tf.paragraphs[0].paragraph_format.alignment = TextAlignment.CENTERUnterstützte Werte: LEFT, CENTER, RIGHT, JUSTIFY.
Wie stelle ich den Zeilenabstand ein?
Verwenden paragraph_format.space_before (Punkte vor dem Absatz) oder paragraph_format.space_after (Punkte nach dem Absatz):
tf.paragraphs[0].paragraph_format.space_before = 12 # 12pt before
tf.paragraphs[0].paragraph_format.space_after = 6 # 6pt after