You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows下获取已安装字体路径/文件名及Python中GDI/DirectWrite实现方法

1. Windows系统中获取所有已安装字体的路径及文件名

方法一:直接访问系统字体目录

Windows已安装字体默认存储在两个核心路径:

  • C:\Windows\Fonts:系统级字体的主目录,绝大多数预装字体都在这里
  • C:\Users\<你的用户名>\AppData\Local\Microsoft\Windows\Fonts:用户自行安装的字体存储位置

打开这些目录后,若默认是字体预览视图,可切换到「详细信息」视图查看完整文件名;也可直接在地址栏输入路径,进入实际文件目录浏览。

方法二:用PowerShell批量导出

打开PowerShell,执行以下命令可快速导出所有已安装字体的名称和路径到CSV文件:

Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts', 'HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts' | ForEach-Object {
    foreach ($value in $_.PSObject.Properties) {
        if ($value.Name -notmatch 'PSPath|PSParentPath|PSChildName|PSDrive|PSProvider' -and $value.Value -ne $null) {
            $fontPath = if ($value.Value -like '*\*') { $value.Value } else { "C:\Windows\Fonts\$($value.Value)" }
            [PSCustomObject]@{
                FontName = $value.Name -replace ' \(TrueType\)| \(OpenType\)'
                FilePath = $fontPath
            }
        }
    }
} | Export-Csv -Path 'C:\已安装字体列表.csv' -Encoding UTF8 -NoTypeInformation

该命令读取注册表中记录的字体信息,自动补全路径并导出为易读的表格格式。


2. Python环境中基于GDI/DirectWrite获取已安装字体路径

方法一:基于GDI API(使用pywin32)

依赖pywin32库,先安装:

pip install pywin32

通过调用Windows GDI的EnumFontFamiliesEx枚举字体,再解析字体数据获取文件路径:

import win32gui
import win32api
import win32con

font_records = []

def font_enum_callback(logfont, textmetric, font_type, extra):
    font_name = logfont.lfFaceName
    hfont = win32gui.CreateFontIndirect(logfont)
    hdc = win32gui.GetDC(0)
    old_font = win32gui.SelectObject(hdc, hfont)
    
    try:
        # 读取字体偏移表获取文件名位置
        offset_buf = win32api.GetFontData(hdc, win32con.OFFSET_TABLE, 0, 4)
        if offset_buf:
            name_offset = int.from_bytes(offset_buf, byteorder='big')
            name_buf = win32api.GetFontData(hdc, name_offset + 6, 0, 256)
            if name_buf:
                name_len = int.from_bytes(name_buf[:2], byteorder='big')
                file_name = name_buf[2:2+name_len].decode('utf-16')
                # 补全路径(如果是系统字体目录下的文件)
                file_path = file_name if '\\' in file_name else f"C:\\Windows\\Fonts\\{file_name}"
                font_records.append({"font_name": font_name, "file_path": file_path})
    except Exception:
        pass
    finally:
        win32gui.SelectObject(hdc, old_font)
        win32gui.DeleteObject(hfont)
        win32gui.ReleaseDC(0, hdc)
    return True

# 初始化LOGFONT结构体,开始枚举
logfont = win32gui.LOGFONT()
logfont.lfCharSet = win32con.DEFAULT_CHARSET
win32gui.EnumFontFamiliesEx(win32gui.GetDC(0), logfont, font_enum_callback, None)

# 去重后输出结果
unique_fonts = {f['font_name']: f for f in font_records}.values()
for font in unique_fonts:
    print(f"{font['font_name']}: {font['file_path']}")

方法二:基于DirectWrite API(使用winrt)

DirectWrite是Windows 10+推荐的现代字体API,无需手动解析二进制数据,更简洁。先安装依赖:

pip install winrt

通过FontManager直接枚举字体并获取文件路径:

from winrt.windows.graphics.directwrite import FontManager

# 获取系统字体管理器
font_manager = FontManager.get_for_current_view()
system_fonts = font_manager.system_font_collection

# 遍历所有字体家族
for idx in range(system_fonts.font_family_count):
    font_family = system_fonts.get_font_family(idx)
    # 获取本地化字体名称(优先中文, fallback 英文)
    family_name = font_family.family_names.lookup_locale_name("zh-CN") or font_family.family_names.lookup_locale_name("en-US")
    # 遍历家族下的每个字体文件
    for font_idx in range(font_family.font_count):
        font = font_family.get_font(font_idx)
        font_file = font.font_files[0]
        print(f"字体家族: {family_name}, 文件路径: {font_file.file_path}")

内容的提问来源于stack exchange,提问作者jeremie bergeron

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 20:03:19