英文:
Difficulty Importing Fonts in Python FPDF package
问题
我在尝试使某些东西工作时遇到了困难,我认为应该很简单。
我是Python的新手,通过项目来学习,在这种情况下,我正在制作一个简单的PDF生成器。我想将自定义字体添加到我的程序中(Poppins),如果我将字体包含在与main.py相同的位置,那么可以使其正常工作,但理想情况下,我希望将这些字体存储在一个单独的文件夹中。
我尝试了很多建议,但迄今为止都没有成功。比如:
- 将字体文件路径附加/插入到sys.path中(链接)
- 在主目录中创建一个
__init__.py
文件,其中包含以下内容
import sys
sys.path.insert(1, '.')
- 也尝试在字体目录中创建一个空的
__init__.py
文件
英文:
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.
答案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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论