How to get a list of installed windows fonts using python?
Asked Answered
G

4

9

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

Gautious answered 25/9, 2020 at 19:5 Comment(0)
C
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'
Cita answered 25/9, 2020 at 19:8 Comment(1)
os.listdir(os.path.join(os.environ['WINDIR'],'fonts')) for generic useUr
M
6

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'

If you prefer to get the font filename, you can use FindSystemFontsFilename.

Example from the doc:

from find_system_fonts_filename import AndroidLibraryNotFound, get_system_fonts_filename, FontConfigNotFound, OSNotSupported

try:
    fonts_filename = get_system_fonts_filename()
except (AndroidLibraryNotFound, FontConfigNotFound, OSNotSupported):
    # Deal with the exception
    # OSNotSupported can only happen in Windows, macOS and Android
    #   - Windows Vista SP2 and more are supported
    #   - macOS 10.6 and more are supported
    #   - Android SDK/API 29 and more are supported
    # FontConfigNotFound can only happen on Linux when Fontconfig could't be found.
    # AndroidLibraryNotFound can only happen on Android when the android library could't be found.
    pass
Maybe answered 13/5, 2022 at 13:7 Comment(0)
P
2

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

Patinated answered 19/3, 2023 at 18:5 Comment(0)
H
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
Hooknosed answered 15/8, 2021 at 23:31 Comment(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!Patinated

© 2022 - 2024 — McMap. All rights reserved.