Wie man Text in Python formatiert
Aspose.Slides FOSS for Python bietet eine feinkörnige Textformatierung über die PortionFormat-Klasse. Ein Portion ist die kleinste unabhängige Texteinheit; er entspricht einem einzelnen Formatierungslauf innerhalb eines Absatzes. Dieser Leitfaden zeigt, wie man Fett‑, Kursiv‑, Schriftgrößen‑ und Farbformatierungen auf Text in einer Präsentation anwendet.
Schritt-für-Schritt-Anleitung
Schritt 1: Installieren Sie das Paket
pip install aspose-slides-fossSchritt 2: Eine Form mit einem Textfeld hinzufügen
Bevor Text formatiert wird, muss eine Form ein TextFrame enthalten. Verwenden Sie shape.add_text_frame(), um eine 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: Zugriff auf das TextFrame
shape.add_text_frame() gibt das TextFrame‑Objekt zurück. Sie können es später auch über shape.text_frame abrufen.
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 Sie portion_format.font_bold und portion_format.font_italic. Diese Eigenschaften akzeptieren NullableBool.TRUE, NullableBool.FALSE oder NullableBool.NOT_DEFINED (erben vom 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)Schritt 5: Schriftgröße und Farbe festlegen
Setze portion_format.font_height für die Größe (in Punkten) und verwende 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 Abschnitte in einem Absatz
Ein einzelner Absatz kann mehrere Abschnitte mit unterschiedlicher Formatierung enthalten. Fügen Sie ein neues Portion zu einer portions‑Sammlung eines Absatzes hinzu:
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, obwohl die Farbe eingestellt wurde
Stellen Sie sicher, dass fill_format.fill_type = FillType.SOLID gesetzt ist, bevor Sie die Farbe zuweisen. 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. Das Zuweisen von Python-True kann eine TypeError auslösen oder stillschweigend nichts tun, abhängig von der Bindung.
Schriftart erscheint nicht in der gespeicherten Datei
Die latin_font-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?
Menge portion_format.latin_font:
fmt.latin_font = slides.FontData("Arial")FontData akzeptiert den Schriftfamiliennamen als Zeichenkette.
Wie stelle ich die Absatzausrichtung ein?
Verwenden Sie 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 Sie 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