【发布时间】:2021-01-12 03:21:03
【问题描述】:
愚蠢的问题,但是如何获得我计算机系统上所有字体名称的列表?
【问题讨论】:
标签: python operating-system os.path listdir
愚蠢的问题,但是如何获得我计算机系统上所有字体名称的列表?
【问题讨论】:
标签: python operating-system os.path listdir
这只是列出Windows\fonts中的文件的问题:
import os
print(os.listdir(r'C:\Windows\fonts'))
【讨论】:
os.listdir(os.path.join(os.environ['WINDIR'],'fonts')) 通用
上面的答案将列出来自/Windows/fonts dir 的所有字体路径。但是,有些人可能还想获得the font title, its variations i.e., thin, bold, and font file name etc? 这是它的代码。
import sys, subprocess
_proc = subprocess.Popen(['powershell.exe', 'Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"'], stdout=sys.stdout)
_proc.communicate()
现在,对于需要字体路径的人。这是使用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
【讨论】: