Skip to content

Instantly share code, notes, and snippets.

@pklaus
Forked from starrhorne/gist:1637310
Last active February 21, 2026 20:42
Show Gist options
  • Select an option

  • Save pklaus/dce37521579513c574d0 to your computer and use it in GitHub Desktop.

Select an option

Save pklaus/dce37521579513c574d0 to your computer and use it in GitHub Desktop.
Extracting font names from TTF/OTF files using Python and fontTools
#!/usr/bin/env python
"""
From
https://github.com/gddc/ttfquery/blob/master/ttfquery/describe.py
and
http://www.starrhorne.com/2012/01/18/how-to-extract-font-names-from-ttf-files-using-python-and-our-old-friend-the-command-line.html
ported to Python 3
"""
import sys
from fontTools import ttLib
FONT_SPECIFIER_NAME_ID = 4
FONT_SPECIFIER_FAMILY_ID = 1
def shortName( font ):
"""Get the short name from the font's names table"""
name = ""
family = ""
for record in font['name'].names:
if b'\x00' in record.string:
name_str = record.string.decode('utf-16-be')
else:
name_str = record.string.decode('utf-8')
if record.nameID == FONT_SPECIFIER_NAME_ID and not name:
name = name_str
elif record.nameID == FONT_SPECIFIER_FAMILY_ID and not family:
family = name_str
if name and family: break
return name, family
tt = ttLib.TTFont(sys.argv[1])
print("Name: %s Family: %s" % shortName(tt))
@Winand
Copy link

Winand commented Feb 21, 2026

from fontTools.ttLib import TTFont

if __name__ == "__main__":
    # font = TTFont("materialdesignicons-webfont.woff2")
    font = TTFont(
        r"fontawesome-free-7.2.0-desktop\otfs\Font Awesome 7 Free-Solid-900.otf"
    )
    print(font["name"].getBestFamilyName())  # "Font Awesome 7 Free"
    print(font["name"].getBestSubFamilyName())  # "Solid"
    print(font["name"].getBestFullName())  # "Font Awesome 7 Free Solid"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment