Md. Zakir Hossan
Md. Zakir Hossan

Reputation: 537

How to read and print the contents of a ttf file?

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

Upvotes: 5

Views: 21227

Answers (2)

Jamilah Foucher
Jamilah Foucher

Reputation: 21

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.

Upvotes: 0

everestial
everestial

Reputation: 7255

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

Upvotes: 11

Related Questions