Here's a sample script:
import os
import sys
import pathlib
import json
from contextlib import redirect_stderr
from fontTools import ttLib
from fontmeta import FontMeta
# Check for commandline argument
if len(sys.argv) == 1:
print('No argument was supplied.')
exit(0)
fontfile = sys.argv[1]
def font_name(font_path, name_idx):
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')
# details[4] = Full Name
# details[1] = Family Name
# details[2] = Style Name
return details[name_idx]
meta_instance = FontMeta(fontfile)
metadata = meta_instance.get_full_data()
fontFullName = font_name(fontfile,4)
fontFamily = font_name(fontfile,1)
fontStyle = font_name(fontfile,2)
fontVers = metadata[5]['value'];
fontVers = fontVers.replace('Version ',"v")
fontLang = metadata[1]['language']['value'];
fontUniqueID = metadata[3]['value']
fontPostscriptName = metadata[6]['value']
fontPostscriptEncoding = metadata[6]['encoding']['value']
fontDesigner = metadata[9]['value']
fontLicenseURL = metadata[14]['value']
print('Full Name: ' + fontFullName)
print('Family: ' + fontFamily)
print('Style: ' + fontStyle)
print('Version: ' + fontVers)
print('Language: ' + fontLang)
print('UniqueID: ' + fontUniqueID)
print('License URL: ' + fontLicenseURL)
print('Font Designer: ' + fontDesigner)
Output:
Full Name: Sharp Sans Bold
Family: Sharp Sans
Style: Bold
Version: v1.001
Language: English/United States
UniqueID: 1.001;2016;SHRP;SharpSans-Bold
License URL: http://www.sharptype.co
Font Designer: Lucas Sharp
ps1:
& "D:\Dev\Python\00 VENV\FontTools\Scripts\Activate.ps1"
$val = python "D:\Dev\Python\Font Scripts\GetFontInfo.py" "D:\Fonts\00 Test\SharpSans-Bold.otf"
Write-Host "`$val:" $val -ForegroundColor Green
Right now the Python code is just printing values. My PS script is echoing the printed values as a string. Is there a way to pass these values to powershell other than just printing them - I.E. as an array?
Or should I return JSON and parse it in PowerShell?
Any help appreciated.