Python 版本的 AddFontResource()

1 python windows powershell fonts

我目前正在尝试在一堆服务器上安装字体。我已经能够使用脚本复制字体并将其“安装”到服务器上,但我需要能够访问这些字体,而无需关闭服务器或注销帐户。

我发现 WindowsAddFontResource()是使用 C++ 完成的,但是 Python 或 Powershell 中有等效的函数吗?

(我一直在使用Python和Powershell来进行检查和安装。)

Mik*_*ike 5

您尝试过使用win32api图书馆吗?它具有可以与inSendMessage()结合使用的功能windll.gdi32.AddFontResource()ctypes

例如安装 TTF 字体文件:

import win32api
import ctypes
import win32con


ctypes.windll.gdi32.AddFontResourceA("C:\\Path\\To\\Font\\font.ttf")
win32api.SendMessage(win32con.HWND_BROADCAST, win32con.WM_FONTCHANGE)
Run Code Online (Sandbox Code Playgroud)

  • 请避免使用旧版 [A]NSI API。您不必要地限制路径,该路径应该能够包含任何 Unicode 字符,而且编写的代码在 Python 3 中不起作用。使用 `gdi32 = ctypes.WinDLL('gdi32');` `gdi32.AddFontResourceW。 argtypes = (ctypes.c_wchar_p,);``gdi32.AddFontResourceW(r"C:\Path\To\Font\font.ttf")`。我定义了“argtypes”以允许 Python 2 中的 ctypes 使用“mbcs”编解码器自动转换字节字符串路径。对于完整的字符范围,请使用 `u""` 文字。 (3认同)