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 Einheit des Textes; es erfasst eine einzige Formatierung innerhalb eines Absatzes. Dieser Leitfaden zeigt, wie man Fett-, Kursiv- und Farbformatierungen auf den Text in einer Präsentation anwenden kann.
Schritt für Schritte
Schritt 1: Installieren des Pakets
pip install aspose-slides-fossSchritt 2: Fügen Sie eine Form mit einem Textrahmen hinzu
Vor dem Formatieren von Text muss eine Form einen TextFrame.- Verwenden . shape.add_text_frame() um einen zu schaffen.
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 den Textrahmen
shape.add_text_frame() gibt den TextFrame Objekt. Sie können es auch später über shape.text_frame.
tf = shape.text_frame # if the frame already exists
tf = shape.add_text_frame("") # creates a new frameA) Die TextFrame enthält eine Liste von Paragraph Objekte (tf.paragraphs) Jeder einzelne. Paragraph enthält: Portion Objekte (paragraph.portions).
Schritt 4: Fett- und Kursivformatierung anwenden
Verwendung portion_format.font_bold und . portion_format.font_italic.Diese Eigenschaften akzeptieren die NullableBool.TRUE, NullableBool.FALSE,oder , NullableBool.NOT_DEFINED (Erbe vom Meister).
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: Fontskala und Farbe festlegen
Setz portion_format.font_height für Größe (in Punkten) und Verwendung fill_format für 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 0255 für jeden Kanal.
Schritt 6: Mehrfach in einem Absatz
Ein einzelner Absatz kann mehrere Teile mit unterschiedlicher Formatierung enthalten. Portion Die Kommission hat die 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 auch nach Einstellung der Farbe schwarz
Sicherstellen fill_format.fill_type = FillType.SOLID ohne den Fülltyp zu setzen, hat die Farbänderung möglicherweise keine Wirkung.
NullableBool.TRUE gegen True
portion_format.font_bold Erwartungen NullableBool.TRUE,Nicht der Python . True.Zuordnung von Python True kann eine TypeError oder nichts tun, je nachdem wie es ist.
Schriftart nicht in der gespeicherten Datei angezeigt
Die latin_font Die Eigenschaft setzt die lateinische Schriftfamilie ein. Wenn nicht festgelegt, wird die Präsentations-Themenschrift verwendet. Benutzerdefinierte Schriften müssen eingebettet oder auf der Betrachtungsmaschine verfügbar sein.
Häufig gestellte Fragen
Wie ändere ich die Schriftfamilie?
Setz portion_format.latin_font:
fmt.latin_font = slides.FontData("Arial")FontData Akzeptiert den Schriftfamiliennamen als Zeichenfolge.
Wie kann ich die Absätze ausrichten?
Verwendung paragraph_format.alignment:
from aspose.slides_foss import TextAlignment
tf.paragraphs[0].paragraph_format.alignment = TextAlignment.CENTERUnterstützte Werte: LEFT, CENTER, RIGHT, JUSTIFY.
Wie kann ich den Linienabstand einstellen?
Verwendung paragraph_format.space_before (Punkte vor Absatz) oder paragraph_format.space_after (Punkte nach Absatz):
tf.paragraphs[0].paragraph_format.space_before = 12 # 12pt before
tf.paragraphs[0].paragraph_format.space_after = 6 # 6pt after