How to Run a Python Script in FontForge
Running Python scripts in FontForge allows you to automate repetitive type design tasks, generate font files, and manipulate glyph data programmatically. FontForge comes with its own embedded Python interpreter and API, which means you can execute Python scripts either directly through the FontForge graphical user interface (GUI) or via your operating system’s terminal or command prompt.
Method 1: Using the FontForge GUI
If you are already working inside the FontForge application, you can execute code snippets or script files directly through the interface.
- Launch FontForge and open an existing font or create a new one.
- Click on File in the top menu bar.
- Select Execute Script... from the dropdown menu (or
press
Ctrl + \on Windows/Linux,Cmd + \on macOS). - In the dialog box that appears, select the Python radio button to ensure the interpreter parses Python syntax instead of the legacy FontForge script.
- Paste your Python code into the text area.
- Click OK to run the script.
To open an interactive Python console inside the GUI, navigate to Window > Python Window. This provides a real-time REPL where you can enter commands line-by-line and inspect font properties dynamically.
Method 2: Using the Command Line (Recommended for Full Scripts)
The most efficient way to run standalone .py files is
through the command line using FontForge’s binary. This runs the script
in a headless environment without launching the full user interface.
Open your terminal or command prompt and run the following command:
fontforge -script /path/to/your_script.pyIf your operating system defaults to legacy FontForge scripting, explicitly specify the language flag:
fontforge -lang=py -script your_script.pyYou can pass command-line arguments to your script by appending them to the end of the command:
fontforge -script your_script.py input.ttf output.otfInside your Python script, these arguments can be read normally using
the standard sys.argv list.
Method 3: Importing FontForge into a Standard Python Environment
If your system's package manager or installer configured the
FontForge Python extension (fontforge.so or
fontforge.pyd) within your system's Python
site-packages, you can run scripts using your normal Python
interpreter.
Create a standard script (script.py) starting with:
import fontforge
# Open an existing font
font = fontforge.open("MyFont.ttf")
# Perform an operation
font.fontname = "MyFont-Updated"
# Generate a new font file
font.generate("MyFont-Updated.otf")
font.close()Run it via standard Python:
python3 script.pyIf you receive an
ImportError: No module named fontforge, use Method
2, as your system requires the script to be invoked through the
fontforge executable directly.