在Python FPDF包中导入字体时遇到的困难。

huangapple go评论68阅读模式
英文:

Difficulty Importing Fonts in Python FPDF package

问题

我在尝试使某些东西工作时遇到了困难,我认为应该很简单。

我是Python的新手,通过项目来学习,在这种情况下,我正在制作一个简单的PDF生成器。我想将自定义字体添加到我的程序中(Poppins),如果我将字体包含在与main.py相同的位置,那么可以使其正常工作,但理想情况下,我希望将这些字体存储在一个单独的文件夹中。

我尝试了很多建议,但迄今为止都没有成功。比如:

  • 将字体文件路径附加/插入到sys.path中(链接
  • 在主目录中创建一个__init__.py文件,其中包含以下内容
    import sys
    sys.path.insert(1, '.')
  • 也尝试在字体目录中创建一个空的__init__.py文件

我附上了我的代码截图以及错误消息。提前感谢您的支持。
在Python FPDF包中导入字体时遇到的困难。

英文:

I'm having a tough time trying to get something working which I think should be straightforward.

I'm new to python and learning via projects which in this case is a simple PDF generator. I want to add custom fonts to my program (Poppins) and I can get it to work if I include the fonts in the exact location as main.py, but ideally, I want these stored in a separate folder.

I've tried numerous suggestions but nothing has worked for me yet. Such as:

  • appending/inserting the font filepath to sys.path (link)
  • creating an __init__.py file in the main directory with the following
    import sys
    sys.path.insert(1, '.')
  • Also tried a blank __init__.py file in the font directory

I've attached a screenshot of my code below along with the error message. Thanks in advance for any support.
在Python FPDF包中导入字体时遇到的困难。

答案1

得分: 1

从FPDF的add_font文档中:

注意:字体源文件必须是可访问的。它们将依次在以下位置搜索(如果这些常量被定义):

FPDF_FONTPATH(默认情况下,在fpdf包目录中的字体文件夹)

SYSTEM_TTFONTS(例如,C:\WINDOWS\FONTS)

英文:

From the add_font documentation of FPDF:

Note: the font source files must be accessible. They are searched successively in (if these constants are defined):

FPDF_FONTPATH (by default, the font folder in the fpdf package directory)

SYSTEM_TTFONTS (e.g. C:\WINDOWS\FONTS)

huangapple
  • 本文由 发表于 2023年1月9日 02:35:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定