Last active
March 31, 2023 07:18
-
-
Save tsukumijima/84657a2750dd7e958b7de4f8d984870f to your computer and use it in GitHub Desktop.
Tool to rename and extract TypeKit (Adobe Fonts) fonts by font name
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
import os | |
import shutil | |
import xmltodict | |
from glob import glob | |
from pathlib import Path | |
# パス | |
typekit_xml_path = Path(os.environ['APPDATA'].replace('\\', '/') + '/Adobe/CoreSync/plugins/livetype/c/entitlements.xml') | |
typekit_font_folder = Path(os.environ['APPDATA'].replace('\\', '/') + '/Adobe/CoreSync/plugins/livetype/r') | |
extract_font_folder = Path(os.environ['USERPROFILE'].replace('\\', '/') + '/Documents/TypeKit') | |
print('Extracting TypeKit...') | |
# 抽出先のフォルダがなければ作成 | |
extract_font_folder.mkdir(parents = True, exist_ok = True) | |
# XML からフォント一覧を取得 | |
fonts_data = xmltodict.parse(open(typekit_xml_path, 'r', encoding = 'utf-8').read())['typekitSyncState']['fonts']['font'] | |
# ファイル一覧 | |
fonts_raw = glob(str(typekit_font_folder) + '/*') | |
# ファイルごとに実行 | |
for font_raw in fonts_raw: | |
# フォントのID | |
font_id = os.path.basename(font_raw) | |
# フォントリストからIDに合うものを探す | |
font_name: str = '' | |
for font_data in fonts_data: | |
if font_data['id'] == font_id: | |
# 新しいファイル名 | |
if 'i18n' in font_data['properties']: | |
# 日本語のフォント名があればそれを使う | |
font_name = font_data['properties']['i18n']['locales']['locale']['fullName'] | |
else: | |
font_name = font_data['properties']['fullName'] | |
break | |
if font_name == '': | |
# フォントが見つからなかった場合はスキップ | |
print(f'FontID: {font_id} Not found. (Skipped)') | |
continue | |
# 移動するパス | |
# フォルダ名に使えない文字を置換 | |
font_file_name = (font_name + '.otf').replace('/', '/').replace('\\', '\') | |
print(f'FontID: {font_id} Saved {font_name} ({font_file_name})') | |
# コピーを実行 | |
font_file_path = extract_font_folder / font_file_name | |
count = 1 | |
while font_file_path.exists(): | |
# ファイルが存在する場合はファイル名に連番を付ける | |
font_file_path = extract_font_folder / font_file_name.replace('.otf', f' ({count}).otf') | |
count += 1 | |
shutil.copyfile(font_raw, font_file_path) | |
print(f'Extracted. ({len(fonts_raw)} fonts)') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment