Babelfont

raw JSON →
3.1.3 verified Fri May 01 auth: no python

A Python library to load, examine, and save fonts in multiple formats (OpenType, TrueType, UFO, etc.) with a unified object model. Version 3.1.3; released irregularly, with breaking changes in major versions.

pip install babelfont
error AttributeError: module 'babelfont' has no attribute 'Font'
cause In v3.x the main class was renamed to Babelfont; 'Font' was removed.
fix
Use from babelfont import Babelfont or use the load function.
error AttributeError: 'int' object has no attribute 'name'
cause Trying to iterate over font.glyphs as a list, but it's a dict-like mapping; integer indices are not valid.
fix
Iterate over glyph names: for glyph_name in font.glyphs: ... or use glyph_obj = font.glyphs[glyph_name].
error KeyError: 'A'
cause Glyph names are case-sensitive; the glyph does not exist.
fix
Check glyph names with list(font.glyphs.keys()) and match exact casing.
breaking v3.0 replaced the old Font object with Babelfont class; direct instantiation of Font is removed.
fix Use from babelfont import Babelfont or the load function.
breaking The glyphs attribute is now a dict-like mapping, not a list. Indexing with integer may fail.
fix Access glyphs by name: font.glyphs['A']. For iteration, use list(font.glyphs.values()).
gotcha Babelfont may emit many log messages; by default it logs to stderr. This clutters output in scripts.
fix Disable logging: import logging; logging.getLogger('babelfont').setLevel(logging.ERROR)
gotcha When loading fonts, some format-specific attributes might be missing; always check with hasattr or dict access.
fix E.g., use getattr(font, 'variations', None) or font.glyphs['A'].get('width')
deprecated The low-level fontforge backend is deprecated and will be removed in v4.0.
fix Switch to the default fonttools backend. Set backend explicitly if needed: load('file', backend='fonttools')

Load a TrueType font, inspect name and glyphs, then export to UFO.

from babelfont import load

# Load a font file
font = load("example.ttf")
# Print font name and glyph count
print(font.name, len(font.glyphs))

# Access a specific glyph
glyph = font.glyphs["A"]
print(glyph.unicode, glyph.width)

# Save as UFO
font.save("example.ufo")