Last active
October 28, 2020 15:40
-
-
Save leoblum/582618276f7df11a18cdf8f874734095 to your computer and use it in GitHub Desktop.
Extract font name from TTF file by python3 + fonttools
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# python 3.8 (will work for lower versions too). | |
# requirements: pip3 install fonttools | |
import os | |
from contextlib import redirect_stderr | |
from fontTools import ttLib | |
def font_name(font_path): | |
font = ttLib.TTFont(font_path, ignoreDecompileErrors=True) | |
with redirect_stderr(None): | |
names = font['name'].names | |
details = {} | |
for x in names: | |
if x.langID == 0 or x.langID == 1033: | |
try: | |
details[x.nameID] = x.toUnicode() | |
except UnicodeDecodeError: | |
details[x.nameID] = x.string.decode(errors='ignore') | |
return details[4], details[1], details[2] | |
print(font_name('myfont.ttf')) # ('Century Bold Italic', 'Century', 'Bold Italic') – name, family, style |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment