8

How do I get a list of all the font names that are on my computer's system?

qubodup
  • 8,687
  • 5
  • 37
  • 45
Paitor
  • 251
  • 2
  • 18

4 Answers4

6

This is just a matter of listing the files in Windows\fonts:

import os

print(os.listdir(r'C:\Windows\fonts'))

The output is a list that starts with something that looks like this:

['arial.ttf', 'arialbd.ttf', 'arialbi.ttf', 'cambria.ttc', 'cambriab.ttf'
qubodup
  • 8,687
  • 5
  • 37
  • 45
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
4

You could also use tkinter which would be better than list the font in C:\Windows\Fonts, because windows can also store fonts in %userprofile%\AppData\Local\Microsoft\Windows\Fonts

For example using the following code from List available font families in tkinter:

from tkinter import Tk, font
root = Tk()
print(font.families())

The output is a tuple that starts with something that looks like this:

('Arial', 'Arial Baltic', 'Arial CE', 'Cambria', 'Cambria Math'
qubodup
  • 8,687
  • 5
  • 37
  • 45
1

The above answer is going to list all the fonts path from the /Windows/fonts dir. However, some people might also want to get the font title, its variations i.e., thin, bold, and font file name etc? Here's the code for that.

import sys, subprocess

_proc = subprocess.Popen(['powershell.exe', 'Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"'], stdout=sys.stdout)
_proc.communicate()

Now, for the people who want font paths. Here's the way using pathlib.

import pathlib

fonts_path = pathlib.PurePath(pathlib.Path.home().drive, os.sep, 'Windows', 'Fonts')
total_fonts = list(pathlib.Path(fonts_path).glob('*.fon'))
if not total_fonts:
    print("Fonts not available. Check path?")
    sys.exit()
return total_fonts
Mujeeb Ishaque
  • 2,259
  • 24
  • 16
  • 1
    The first code returns font names but also a strange vertical line of text while the second code doesn't work with Python 3, however many thanks for hinting towards the registry! – qubodup Mar 19 '23 at 17:45
1

The Windows registry apparently keeps track of fonts:

import winreg

reg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)

key = winreg.OpenKey(reg, r'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts',
    0, winreg.KEY_READ)

for i in range(0, winreg.QueryInfoKey(key)[1]):
    print(winreg.EnumValue(key, i))

The outputs starts with something that looks like this:

('Arial (TrueType)', 'arial.ttf', 1)
('Arial Bold (TrueType)', 'arialbd.ttf', 1)
('Arial Bold Italic (TrueType)', 'arialbi.ttf', 1)
('Cambria & Cambria Math (TrueType)', 'cambria.ttc', 1)
('Cambria Bold (TrueType)', 'cambriab.ttf', 1)

If you want simpler output like:

Arial (TrueType)

Use print(winreg.EnumValue(key, i)[0]) instead of print(winreg.EnumValue(key, i)) in the last line.

This was combined from Mujeeb Ishaque's respons as well as Python code to read registry and Loop through values or registry key.. _winreg Python

qubodup
  • 8,687
  • 5
  • 37
  • 45