fpdf2 错误 参数 "new_x" 的值无效



我试图遵循本教程,特别是Tuto 2,但我一直得到以下错误,我不知道为什么。我复制并粘贴了教程中的代码。我的版本是2.5.2.

ValueError:参数"new_x"值无效(0),必须是实例of Enum XPos

from fpdf import FPDF
class PDF(FPDF):
    def header(self):
        # Rendering logo:
        self.image("logo.png", 10, 8, 33)
        # Setting font: helvetica bold 15
        self.set_font("helvetica", "B", 15)
        # Moving cursor to the right:
        self.cell(80)
        # Printing title:
        self.cell(30, 10, "Title", 1, 0, "C")
        # Performing a line break:
        self.ln(20)
    def footer(self):
        # Position cursor at 1.5 cm from bottom:
        self.set_y(-15)
        # Setting font: helvetica italic 8
        self.set_font("helvetica", "I", 8)
        # Printing page number:
        self.cell(0, 10, f"Page {self.page_no()}/{{nb}}", 0, 0, "C")

# Instantiation of inherited class
pdf = PDF()
pdf.alias_nb_pages()
pdf.add_page()
pdf.set_font("Times", size=12)
for i in range(1, 41):
    pdf.cell(0, 10, f"Printing line number {i}", 0, 1)
pdf.output("tuto2.pdf")

看起来github项目表明您需要为new_xnew_y传入XPos和YPos对象。

首先,在import语句下面添加下面的代码:

from fpdf.enums import XPos, YPos .

这将带来你需要正确设置页码位置的类。

然后更新footer函数-而不是

self.cell(0, 10, f"Page {self.page_no()}/{{nb}}", 0, 0, "C"),使用

self.cell(0, 10, f"Page {self.page_no()}/{{nb}}", XPos.RIGHT, new_y=YPos.NEXT, "C") .

最新更新