Batch Convert Fonts Using FontForge and Python
Yes, you can write a Python script to batch convert font formats using FontForge. FontForge includes built-in Python bindings that expose its core font manipulation engine, allowing you to automate the conversion of multiple font files between formats such as TTF, OTF, WOFF, WOFF2, and SVG. This guide provides a complete, reusable Python script to process an entire directory of fonts and explains how to execute it properly on your system.
Prerequisites
To run a FontForge Python script, you must have FontForge installed.
FontForge packages its own internal Python interpreter or installs the
fontforge Python module into your environment depending on
your operating system:
- Linux: Install via your package manager (e.g.,
sudo apt install fontforge python3-fontforge). - macOS: Install via Homebrew using
brew install fontforge. - Windows: Download the installer from the official
FontForge site. It comes bundled with a dedicated
ffpython.exeexecutable inside the installation folder.
The Batch Conversion Script
Save the following code as convert_fonts.py. This script
scans an input folder for specified font formats and exports each file
to your desired target format.
import os
import sys
import fontforge
def batch_convert(input_dir, output_dir, target_extension):
"""
Converts all supported fonts in input_dir to target_extension in output_dir.
"""
# Supported input extensions
valid_extensions = ('.ttf', '.otf', '.woff', '.woff2', '.pfa', '.pfb')
target_extension = target_extension.lower().lstrip('.')
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for filename in os.listdir(input_dir):
base_name, ext = os.path.splitext(filename)
if ext.lower() in valid_extensions:
input_path = os.path.join(input_dir, filename)
output_filename = f"{base_name}.{target_extension}"
output_path = os.path.join(output_dir, output_filename)
try:
print(f"Converting: {filename} -> {output_filename}")
font = fontforge.open(input_path)
font.generate(output_path)
font.close()
except Exception as e:
print(f"Failed to convert {filename}: {e}", file=sys.stderr)
print("Batch conversion completed.")
if __name__ == "__main__":
if len(sys.argv) < 4:
print("Usage: fontforge -script convert_fonts.py <input_folder> <output_folder> <target_format>")
print("Example: fontforge -script convert_fonts.py ./input ./output woff2")
sys.exit(1)
in_folder = sys.argv[1]
out_folder = sys.argv[2]
target_format = sys.argv[3]
batch_convert(in_folder, out_folder, target_format)How to Run the Script
Because FontForge scripts often rely on FontForge's compiled
libraries, the most reliable way to execute the script is through the
FontForge binary using the -script flag.
Linux and macOS
Open your terminal and run:
fontforge -script convert_fonts.py /path/to/input /path/to/output woff2If you installed python3-fontforge directly into your
system Python environment on Linux, you can also run it normally:
python3 convert_fonts.py /path/to/input /path/to/output ttfWindows
On Windows, use the bundled ffpython.exe interpreter
located in the FontForge directory:
"C:\Program Files (x86)\FontForgeBuilds\bin\ffpython.exe" convert_fonts.py C:\fonts\input C:\fonts\output woffSupported Output Formats
FontForge determines the output format from the file extension passed
to the font.generate() method. You can specify any of the
following standard extensions:
ttf(TrueType)otf(OpenType / CFF)woff(Web Open Font Format)woff2(Web Open Font Format 2.0)svg(Scalable Vector Graphics Font)
Using this script ensures rapid, consistent font conversions without the need for manual, GUI-based exports or third-party web converters.