How to get a list of installed windows fonts using python?
Question:
How do I get a list of all the font names that are on my computer’s system?
Answers:
This is just a matter of listing the files in Windowsfonts
:
import os
print(os.listdir(r'C:Windowsfonts'))
The output is a list that starts with something that looks like this:
['arial.ttf', 'arialbd.ttf', 'arialbi.ttf', 'cambria.ttc', 'cambriab.ttf'
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:SOFTWAREMicrosoftWindows NTCurrentVersionFonts"'], 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
You could also use tkinter which would be better than list the font in C:WindowsFonts
, because windows can also store fonts in %userprofile%AppDataLocalMicrosoftWindowsFonts
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'
The Windows registry apparently keeps track of fonts:
import winreg
reg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
key = winreg.OpenKey(reg, r'SOFTWAREMicrosoftWindows NTCurrentVersionFonts',
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
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 Windowsfonts
:
import os
print(os.listdir(r'C:Windowsfonts'))
The output is a list that starts with something that looks like this:
['arial.ttf', 'arialbd.ttf', 'arialbi.ttf', 'cambria.ttc', 'cambriab.ttf'
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:SOFTWAREMicrosoftWindows NTCurrentVersionFonts"'], 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
You could also use tkinter which would be better than list the font in C:WindowsFonts
, because windows can also store fonts in %userprofile%AppDataLocalMicrosoftWindowsFonts
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'
The Windows registry apparently keeps track of fonts:
import winreg
reg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
key = winreg.OpenKey(reg, r'SOFTWAREMicrosoftWindows NTCurrentVersionFonts',
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