Warm tip: This article is reproduced from stackoverflow.com, please click
fonts ttx-fonttools

fonttools convert ttf to woff2

发布于 2020-04-15 10:51:11

Currenly, I'm using fonttools(https://github.com/fonttools/fonttools) to convert font file ttf to woff2 by ttx command with 2 steps

  • convert ttf to ttx
  • then convert ttx to woff2

But it's too slow and ttx file to big, is there any way to convert ttf to woff2 directly by using fonttools to improve performance?

Questioner
Quan Vo
Viewed
72
djangodude 2020-02-04 12:32

With fonttools installed in your Python (virtualenv, pipenv, etc):

$ python
>>> from fontTools.ttLib import TTFont
>>> f = TTFont('path/to/your/file.otf')
>>> f.flavor='woff2'
>>> f.save('path/to/your/file.woff2')

NOTE: you might need to install other fontTools dependencies ('brotli', others) to allow saving with flavor=woff2 to work correctly.