FontData — Aspose.Slides FOSS Python API Reference

FontData is a lightweight, immutable class that wraps a font family name. It is used as the value type for font-related properties on PortionFormat (e.g. latin_font, east_asian_font, complex_script_font).

Package: aspose.slides_foss


FontData

Constructor

from aspose.slides_foss import FontData

font = FontData("Calibri")
font = FontData("Arial")
font = FontData("+mj-lt")   # major Latin theme font token
font = FontData("+mn-lt")   # minor Latin theme font token

FontData Properties

PropertyTypeAccessDescription
font_namestrReadThe font family name (e.g. "Calibri", "Arial", "+mj-lt").

FontData Methods

MethodSignatureDescription
get_font_name(theme) -> strReturn the resolved font name. In this implementation returns font_name regardless of theme.

Using FontData with PortionFormat

FontData is set on a PortionFormat to control which typeface is used for a run of text.

PortionFormat PropertyDescription
latin_fontFont for Latin (Western) characters.
east_asian_fontFont for East Asian characters.
complex_script_fontFont for complex-script languages (Arabic, Hebrew, etc.).

Code Examples

Set the Font for a Text Run

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType, FontData, NullableBool
from aspose.slides_foss.drawing import Color
from aspose.slides_foss import FillType
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, 400, 100)
    tf = shape.add_text_frame("Custom Font Example")

    portion = tf.paragraphs[0].portions[0]
    fmt = portion.portion_format
    fmt.latin_font = FontData("Georgia")
    fmt.font_height = 28
    fmt.font_bold = NullableBool.TRUE
    fmt.fill_format.fill_type = FillType.SOLID
    fmt.fill_format.solid_fill_color.color = Color.from_argb(255, 40, 40, 40)

    prs.save("font-demo.pptx", SaveFormat.PPTX)

Use a Theme Font Token

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType, FontData
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, 400, 100)
    tf = shape.add_text_frame("Theme-font heading")

    portion = tf.paragraphs[0].portions[0]
    # "+mj-lt" resolves to the presentation's major Latin theme font
    portion.portion_format.latin_font = FontData("+mj-lt")
    portion.portion_format.font_height = 32

    prs.save("theme-font.pptx", SaveFormat.PPTX)

Read the Font Name from an Existing Portion

import aspose.slides_foss as slides

with slides.Presentation("deck.pptx") as prs:
    for slide in prs.slides:
        for shape in slide.shapes:
            if not hasattr(shape, 'text_frame') or shape.text_frame is None:
                continue
            for para in shape.text_frame.paragraphs:
                for portion in para.portions:
                    fmt = portion.portion_format
                    latin = fmt.latin_font
                    if latin is not None:
                        print(f"  Font: {latin.font_name!r}  "
                              f"size={fmt.font_height}  "
                              f"bold={fmt.font_bold}  "
                              f"text={portion.text!r}")

Apply Different Fonts to Multiple Portions

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType, FontData, NullableBool
from aspose.slides_foss.drawing import Color
from aspose.slides_foss import FillType
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, 80)
    tf = shape.add_text_frame("")
    para = tf.paragraphs[0]

    # Clear auto-created portion
    while len(para.portions) > 0:
        para.portions.remove_at(0)

    def add_run(text, font_name, size, bold=False):
        from aspose.slides_foss import Portion
        p = Portion()
        p.text = text
        p.portion_format.latin_font = FontData(font_name)
        p.portion_format.font_height = size
        p.portion_format.font_bold = NullableBool.TRUE if bold else NullableBool.FALSE
        p.portion_format.fill_format.fill_type = FillType.SOLID
        p.portion_format.fill_format.solid_fill_color.color = Color.from_argb(255, 0, 0, 0)
        para.portions.add(p)

    add_run("Title: ", "Arial", 24, bold=True)
    add_run("Body text in Calibri", "Calibri", 20)

    prs.save("mixed-fonts.pptx", SaveFormat.PPTX)

See Also