python-pptx icon indicating copy to clipboard operation
python-pptx copied to clipboard

Get font properties

Open ab-synth opened this issue 7 months ago • 4 comments

Hi guys

Is there a reliable way to get the font properties (font family, size, letter spacing etc.) of the run element? If not then is there a known pptx hierarchy structure that we can iterate on to fetch the font properties from the upper levels?

The alternative is to convert pptx to pdf and read the info from there but that's also complex and pdf often looks different (text goes in multiple lines etc.)

I suppose that something like this is not sufficient?

def _extract_font_family(run: Any, paragraph: Any, shape: Any) -> str:
""" 
    1. The primary source of font information is the run.font.name property, so we check that first
    2. If that's not available, we fall back to the latin typeface
    3. Then we check the paragraph's font
    4. Finally, we check the shape's font
    5. If none of these are available, we use "Arial" as the default fallback
"""
if run.font.name:
    return run.font.name
elif hasattr(run.font, 'latin') and run.font.latin and run.font.latin.typeface:
    return run.font.latin.typeface
elif hasattr(paragraph, 'font') and paragraph.font.name:
    return paragraph.font.name
elif hasattr(shape, 'font') and shape.font.name:
    return shape.font.name

return DEFAULT_FONT_FAMILY

ab-synth avatar May 28 '25 08:05 ab-synth