Python: FPDF 中的着色单元格不起作用?

Python: Coloring cells in FPDF is not working?

我正在使用 FPDF 库创建 PDF,并且我希望为文档中的单元格着色。我查看了 API 并发现这是这样做的方法:

fpdf.set_fill_color(r: int, g: int = -1, b: int = -1)

所以我继续在脚本中执行此操作:

pdf = FPDF()

pdf.add_page()

pdf.set_font('Arial', 'B', 7)
pdf.set_fill_color(0, 0, 255)
pdf.cell(190, 6, 'Testing...', 1, 1, 'L')

pdf.output('Color.pdf', 'F')

而且颜色没有变化。其他一切正常,我只是得到一个白色单元格而不是蓝色单元格。也不会抛出任何错误。是我做错了什么还是这个 PyFPDF 出了故障?

编辑:向这个问题添加了 pdf.add_page()pdf.output('Color.pdf', 'F')(忘了在这里做,在我的脚本中)。

根据 docs 您必须将 fill 设置为 True

[...]

fill:

Indicates if the cell background must be painted (True) or transparent (False). Default value: False.

[...]


from fpdf import FPDF

pdf = FPDF()

pdf.add_page()

pdf.set_font('Arial', 'B', 7)
pdf.set_fill_color(0, 0, 255)
pdf.cell(190, 6, 'Testing...', 1, 1, 'L', fill=True)

pdf.output('Color.pdf', 'F')