Skip to content

Instantly share code, notes, and snippets.

@m4rc1e
Created April 12, 2017 11:03
Show Gist options
  • Save m4rc1e/c1b1682eacba684c55d09958357fc465 to your computer and use it in GitHub Desktop.
Save m4rc1e/c1b1682eacba684c55d09958357fc465 to your computer and use it in GitHub Desktop.
from fontTools.ttLib import TTFont
import csv
import os
import re
from ntpath import basename
gf_dir = '/Users/marc/Documents/googlefonts/fonts'
gf_repo_file = open('/Users/marc/Documents/googlefonts/gf_doc_patches/GF_Upstream_Repos - Sheet1-4.csv')
ofl_doc = csv.DictReader(gf_repo_file)
name_swap = {}
for root, dirs, files in os.walk(gf_dir, topdown=False):
for file in files:
if file.endswith('.ttf'):
font_path = os.path.join(root, file)
font = TTFont(font_path)
ofl_name = basename(root)
enc = font['name'].getName(1, 3, 1, 1033).getEncoding()
family_name = font['name'].getName(1, 3, 1, 1033)
family_name = str(family_name).decode(enc)
name_cleaned = re.sub(r' Thin| Regular| SemiBold| Semibold| Light| SemiLight', '', family_name)
print name_cleaned
name_swap[ofl_name] = name_cleaned
with open('new_csv.csv', 'wb') as f:
writer = csv.DictWriter(f, fieldnames=ofl_doc.fieldnames, delimiter='\t')
for row in ofl_doc:
if row['family'].lower() in name_swap:
ofl_name = row['family'].lower()
row['family'] = name_swap[ofl_name]
print row.values()
writer.writerow(row)
gf_repo_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment