【问题标题】:How to get a list of installed windows fonts using python?如何使用 python 获取已安装的 Windows 字体列表?
【发布时间】:2021-01-12 03:21:03
【问题描述】:

愚蠢的问题,但是如何获得我计算机系统上所有字体名称的列表?

【问题讨论】:

    标签: python operating-system os.path listdir


    【解决方案1】:

    这只是列出Windows\fonts中的文件的问题:

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

    【讨论】:

    • os.listdir(os.path.join(os.environ['WINDIR'],'fonts')) 通用
    【解决方案2】:

    上面的答案将列出来自/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
    

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 2012-01-29
      • 2020-01-20
      • 1970-01-01
      • 2014-04-14
      • 2010-09-10
      • 2015-11-19
      相关资源
      最近更新 更多