How do I get a list of all the font names that are on my computer's system?
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'
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
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
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
© 2022 - 2024 — McMap. All rights reserved.
os.listdir(os.path.join(os.environ['WINDIR'],'fonts'))
for generic use – Ur