Bhavesh Mevada
Bhavesh Mevada

Reputation: 272

How to retrieve actual font file name in python?

My code

import tkinter as tk  
from tkinter import font  
root = tk.Tk()  
fonts = list(font.families())  
fonts.sort()  
print(fonts)
root.mainloop()

Output

['Arial', 'Arial Baltic', 'Arial Black', 'Arial CE', 'Arial CYR', 'Arial Greek', 'Arial Narrow', 'Arial Rounded MT Bold', 'Arial TUR', 'Arial Unicode MS', 'Baskerville Old Face', 'Batang', 'BatangChe', 'Bauhaus 93', 'Bell MT', 'Berlin Sans FB', 'Berlin Sans FB Demi', 'Bernard MT Condensed', 'Blackadder ITC', 'Bodoni MT', 'Bodoni MT Black', 'Bodoni MT Condensed', 'Bodoni MT Poster Compressed', 'Book Antiqua', 'Bookman Old Style', 'Bookshelf Symbol 7', 'Bradley Hand ITC', 'Britannic Bold', 'Broadway', 'Browallia New', 'BrowalliaUPC', 'Brush Script MT', 'Calibri', 'Calibri Light', 'Californian FB', 'Calisto MT', 'Cambria', 'Cambria Math', 'Candara', 'Castellar', 'Centaur', 'Century', 'Century Gothic', 'Century']

But, I want list contains actual name of font file which is shown in font properties.

['arial','arialbi','ariblk',''.........]

Upvotes: 2

Views: 1261

Answers (1)

Bhavesh Mevada
Bhavesh Mevada

Reputation: 272

import os

list = []
for file in os.listdir(r'C:\Windows\Fonts'):
    if file.endswith(".ttf"):
        list.append(file)

print(list)

Above code will generate a list contains font file names.

Upvotes: 1

Related Questions