4

Is there any way that I can open, read and write a ttf file?

Example:

with open('xyz.ttf') as f:
    content = f.readline()
    print(content)

A bit more: If I open a .ttf (font) file with windows font viewer we see the following image a .ttf (font) file open with windows font viewer

From this I like to extract following lines as text, with proper style. portion of .ttf (font) file open with windows font viewer

Md. Zakir Hossan
  • 517
  • 2
  • 6
  • 17
  • 1
    Like, a font file? What would you expect printing the contents to do? – SpoonMeiser Mar 15 '17 at 16:14
  • What is exactly inside this file with *.ttf extension. I think you need to add more details of the `input` and `output`. If you reffering to a `font` type database you must first find a module/package to open and read it. – everestial007 Mar 15 '17 at 16:37
  • Ok, you provided the input. But, as I suggested in the answer you need to install `FontTools 3.9.0` as it is not a regular text file. This package works with any python version so its good. You really need to do some reading of the documentation - no offense intended. – everestial007 Mar 15 '17 at 17:08

2 Answers2

11

What is exactly inside this file with *.ttf extension. I think you need to add more details of the input and output. If you reffering to a font type database you must first find a module/package to open and read it, since *.ttf isn't a normal text file.

Read the given links and install the required packages first:

https://pypi.python.org/pypi/FontTools

Then, as suggested:

from fontTools.ttLib import TTFont
font = TTFont('/path/to/font.ttf')
print(font)
<fontTools.ttLib.TTFont object at 0x10c34ed50>

If you need help with something else trying putting the input and expected output.

Other links:

http://www.starrhorne.com/2012/01/18/how-to-extract-font-names-from-ttf-files-using-python-and-our-old-friend-the-command-line.html

Here is a another useful python script:

https://gist.github.com/pklaus/dce37521579513c574d0

everestial007
  • 6,665
  • 7
  • 32
  • 72
0

To display the images that are associated with each keyboard character using ttf format, add the ttf file to the path folder for Word/LibreOffice. Then find the name of the ttf file amongst the font file names; this will allow you to manipulate the font images like text in a text document. But if you copy-paste the font images, the Unicode name (which is the keyboard character that is associated with each image) will appear and NOT the image. To change the Unicode name I think you can use fontforge, but I have not yet tried it.