Created
May 12, 2024 08:31
-
-
Save tvdsluijs/6ac11889002cc1d92c7fc86f2bed3fdd to your computer and use it in GitHub Desktop.
Python script to convert TTF and/or OTF font files to WOFF fonts
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
""" | |
Font Converter Script | |
This script converts TrueType Font (TTF) and OpenType Font (OTF) files to Web Open Font Format (WOFF). | |
Usage: | |
1. Set the paths to your TTF font folder and WOFF output folder in the variables `font_folder` and `woff_folder`, respectively. | |
2. Run the script. | |
Dependencies: | |
- This script requires the `pyftsubset` tool from the `fontTools` Python library. | |
- Install fontTools via pip: `pip install fonttools`. | |
Author: Theo van der Sluijs | |
Email: info@itheo.tech | |
Date: 12 May 2024 | |
License: MIT License | |
Project URL: https://tvdsluijs.github.io/python-font-converter | |
Author URL: https://itheo.tech | |
""" | |
import os | |
import subprocess | |
import sys | |
def ttf_to_woff(ttf_path, woff_path): | |
""" | |
Convert a TrueType Font (TTF) file to Web Open Font Format (WOFF) using pyftsubset. | |
Args: | |
ttf_path (str): Path to the input TTF file. | |
woff_path (str): Path to save the output WOFF file. | |
Returns: | |
bool: True if conversion is successful, False otherwise. | |
""" | |
try: | |
# Use pyftsubset to convert TTF to WOFF | |
subprocess.run(['pyftsubset', ttf_path, '--flavor=woff', '--output-file=' + woff_path], check=True, stderr=subprocess.PIPE) | |
return True | |
except subprocess.CalledProcessError as e: | |
print(f"Error converting {ttf_path} to WOFF:") | |
print(e.stderr.decode(sys.getfilesystemencoding()).strip()) | |
return False | |
def otf_to_woff(otf_path, woff_path): | |
""" | |
Convert an OpenType Font (OTF) file to Web Open Font Format (WOFF) using pyftsubset. | |
Args: | |
otf_path (str): Path to the input OTF file. | |
woff_path (str): Path to save the output WOFF file. | |
Returns: | |
bool: True if conversion is successful, False otherwise. | |
""" | |
try: | |
# Use pyftsubset to convert OTF to WOFF | |
subprocess.run(['pyftsubset', otf_path, '--flavor=woff', '--output-file=' + woff_path], check=True, stderr=subprocess.PIPE) | |
return True | |
except subprocess.CalledProcessError as e: | |
print(f"Error converting {otf_path} to WOFF:") | |
print(e.stderr.decode(sys.getfilesystemencoding()).strip()) | |
return False | |
def convert_folder_fonts_to_woff(font_folder, woff_folder): | |
""" | |
Convert all TrueType Font (TTF) and OpenType Font (OTF) files in a folder to Web Open Font Format (WOFF). | |
Args: | |
font_folder (str): Path to the folder containing TTF and/or OTF files. | |
woff_folder (str): Path to save the converted WOFF files. | |
""" | |
ttf_count = 0 # Counter for processed TTF files | |
woff_count = 0 # Counter for created WOFF files | |
# Create the WOFF folder if it doesn't exist | |
if not os.path.exists(woff_folder): | |
os.makedirs(woff_folder) | |
# Loop through all font files in the folder | |
for filename in os.listdir(font_folder): | |
if filename.endswith('.ttf'): | |
ttf_count += 1 | |
font_path = os.path.join(font_folder, filename) | |
woff_filename = os.path.splitext(filename)[0] + '.woff' | |
woff_path = os.path.join(woff_folder, woff_filename) | |
try: | |
if ttf_to_woff(font_path, woff_path): | |
woff_count += 1 | |
except subprocess.CalledProcessError: | |
otf_path = os.path.join(font_folder, os.path.splitext(filename)[0] + '.otf') | |
if os.path.exists(otf_path): | |
ttf_count -= 1 | |
try: | |
if otf_to_woff(otf_path, woff_path): | |
woff_count += 1 | |
except subprocess.CalledProcessError as e: | |
print(f"Error converting both {filename} (TTF) and {os.path.splitext(filename)[0]}.otf to WOFF:") | |
print(e.stderr.decode(sys.getfilesystemencoding()).strip()) | |
else: | |
print(f"Error converting {filename} (TTF) to WOFF:") | |
# ONLY uncomment this code when you also want to process all remaining OTF files | |
# elif filename.endswith('.otf'): | |
# font_path = os.path.join(font_folder, filename) | |
# woff_filename = os.path.splitext(filename)[0] + '.woff' | |
# woff_path = os.path.join(woff_folder, woff_filename) | |
# try: | |
# if otf_to_woff(font_path, woff_path): | |
# woff_count += 1 | |
# except subprocess.CalledProcessError as e: | |
# print(f"Error converting {filename} (OTF) to WOFF:") | |
# print(e.stderr.decode(sys.getfilesystemencoding()).strip()) | |
print(f"Processed {ttf_count} TTF files") | |
print(f"Created {woff_count} WOFF files") | |
font_folder = "[path to your font ttf folder]" | |
woff_folder = "[path to your output woff folder]" | |
convert_folder_fonts_to_woff(font_folder, woff_folder) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment