Muda42
Muda42

Reputation: 61

How do I convert a woff2 font to ttf using fonttools in Python?

I have woff2 files that I would like to convert to ttf using Python and the fonttools library. I have seen some methods on how to convert ttf fonts to woff2 but I can't use them to do the opposite.

Thanks in advance

Upvotes: 2

Views: 4436

Answers (3)

no_name_pls
no_name_pls

Reputation: 21

Here's the one-line command (on Windows):

python -c "from fontTools.ttLib import woff2; import brotli; woff2.decompress('path/to/your.woff2', 'path/to/output.ttf')"

Make sure you have installed brotli. Use forward slash (/) for the file path instead of backslash (\). The output can be either TTF or OTF.

Upvotes: 2

qareebi
qareebi

Reputation: 71

from fontTools.ttLib import woff2
# you also need brotli
import os

def convert(infilename, outfilename):
    with open(infilename , mode='rb') as infile:
        with open(outfilename, mode='wb') as outfile:
            woff2.decompress(infile, outfile)

Here's a CLI friendly script I wrote https://github.com/basisvectors/woff2otf

python woff2otf.py (filename.woff or filename.woff2 or directory for batch conversion) [target file or dir name]

Upvotes: 3

Muda42
Muda42

Reputation: 61

I figured it out one can use fontTools.ttLib.woff2.decompress() to acheive this

Upvotes: 4

Related Questions