定义字体以在 python fpdf 中重用

问题描述

我正在使用 fpdf 在 python 中构建 PDF 文档。字体设置如下:

pdf.set_font('arial','B',20)  

但是,如果我必须多次更改字体(对于标题、副标题、常规文本等),这似乎有点不清楚。所以我试图以下列形式预先定义字体:

heading = ["arial","B",20]
sub_heading = ["arial",15]

然后使用定义的字体:

pdf.set_font(heading)

不幸的是,这会导致以下错误

 Traceback (most recent call last):
  File "C:\Users\me\PycharmProjects\pythonProject\project\main.py",line 112,in <module>
    pdf.set_font(heading)
  File "C:\Users\me\PycharmProjects\pythonProject\project\venv\lib\site-packages\fpdf\fpdf.py",line 567,in set_font
    family=family.lower()
AttributeError: 'list' object has no attribute 'lower'

所以我想知道,还有其他方法可以实现吗?

解决方法

尝试使用这个:

pdf.set_font(*heading)