【问题标题】:Python3 how to install .ttf font file?Python3如何安装.ttf字体文件?
【发布时间】:2018-12-13 02:52:10
【问题描述】:

我想用 python3(更精确的 Python 3.6)代码在 windows 10 上安装 .ttf 字体文件,我用谷歌搜索,但我唯一找到的是这个Install TTF fonts on windows with python,我测试了它,但它没有做任何事情。有没有办法用python3代码安装.ttf?

提前致谢。

【问题讨论】:

  • 您的系统上有 .ttf 文件吗?
  • 我的文件夹中只有 .ttf,可以安装,但我想用 python 代码安装。

标签: python python-3.x truetype


【解决方案1】:

这可能会对您有所帮助,它会尝试将所有字体安装在一个文件夹中,但您可以将其修改为仅使用 install_font 函数安装 1 种字体:https://gist.github.com/tushortz/598bf0324e37033ed870c4e46461fb1e

import os
import shutil
import ctypes
from ctypes import wintypes
import sys
import ntpath
try:
    import winreg
except ImportError:
    import _winreg as winreg

user32 = ctypes.WinDLL('user32', use_last_error=True)
gdi32 = ctypes.WinDLL('gdi32', use_last_error=True)

FONTS_REG_PATH = r'Software\Microsoft\Windows NT\CurrentVersion\Fonts'

HWND_BROADCAST = 0xFFFF
SMTO_ABORTIFHUNG = 0x0002
WM_FONTCHANGE = 0x001D
GFRI_DESCRIPTION = 1
GFRI_ISTRUETYPE = 3

def install_font(src_path):
    # copy the font to the Windows Fonts folder
    dst_path = os.path.join(os.environ['SystemRoot'], 'Fonts',
                            os.path.basename(src_path))
    shutil.copy(src_path, dst_path)
    # load the font in the current session
    if not gdi32.AddFontResourceW(dst_path):
        os.remove(dst_path)
        raise WindowsError('AddFontResource failed to load "%s"' % src_path)
    # notify running programs
    user32.SendMessageTimeoutW(HWND_BROADCAST, WM_FONTCHANGE, 0, 0,
                               SMTO_ABORTIFHUNG, 1000, None)
    # store the fontname/filename in the registry
    filename = os.path.basename(dst_path)
    fontname = os.path.splitext(filename)[0]
    # try to get the font's real name
    cb = wintypes.DWORD()
    if gdi32.GetFontResourceInfoW(filename, ctypes.byref(cb), None,
                                  GFRI_DESCRIPTION):
        buf = (ctypes.c_wchar * cb.value)()
        if gdi32.GetFontResourceInfoW(filename, ctypes.byref(cb), buf,
                                      GFRI_DESCRIPTION):
            fontname = buf.value
    is_truetype = wintypes.BOOL()
    cb.value = ctypes.sizeof(is_truetype)
    gdi32.GetFontResourceInfoW(filename, ctypes.byref(cb),
                               ctypes.byref(is_truetype), GFRI_ISTRUETYPE)
    if is_truetype:
        fontname += ' (TrueType)'
    with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, FONTS_REG_PATH, 0,
                        winreg.KEY_SET_VALUE) as key:
        winreg.SetValueEx(key, fontname, 0, winreg.REG_SZ, filename)

本质上,使用 Windows 的 gdi32 API 中的 AddFontResourceW 加载字体并使用 SendMessage API 通知正在运行的程序 - 这将使字体在正在运行的程序中可见。

要将字体永久安装到 Windows 中,您需要将字体文件复制到 Fonts 文件夹(C:\Windows\Fonts,或 %userprofile%\AppData\Local\Microsoft\Windows\Fonts 用于每个用户文件夹)和然后在 HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Fonts 的注册表中添加一个键(或 HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts 用于每个用户安装)。

【讨论】:

  • 虽然这可能会回答问题,但it would be preferable 将在此处包含答案的基本部分,并提供链接以供参考。
【解决方案2】:

This 库看起来很有希望(我自己没试过)。

正在安装

pip install --user fonttools

pip3 install --user fonttools

代码

from fontTools.ttLib import TTFont
font = TTFont('/path/to/font.ttf')

然后使用font.save方法:

定义:font.save(self, file, reorderTables=True)

文档字符串:保存 字体到磁盘。与构造函数类似,“文件”参数 可以是路径名或可写文件对象。

【讨论】:

  • 我在 Windows 上,当我尝试在 Visual Code 上运行它时出现错误:ImportError: No module named fontTools.ttLib
  • 你的环境中安装了库吗?
  • 现在 import 和 font = TTFont('/path/to/font.ttf') 的部分正在工作,但现在还有另一个问题,那就是当我尝试 font.save 给我一个错误 NameError: name 'self' is未定义
  • .save 是一种方法。你需要这样称呼它:font.save("path/to/save/")
  • 我很难阅读文档。有什么更简单的替代方法吗?
猜你喜欢
  • 2023-03-25
  • 1970-01-01
  • 2016-02-25
  • 2013-11-15
  • 2021-04-22
  • 2017-07-14
  • 2012-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多