fpdf2错误:参数“new_x”的值无效。

3
我正在尝试跟随这个tutorial,具体来说是Tuto 2,但是我一直收到以下错误,并且不知道为什么。我从教程中复制并粘贴了代码。我使用的是2.5.2版本。

ValueError: 参数“new_x”的值无效(0),必须是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")
1个回答

4

看起来这个 Github 项目表明,你需要传入一个XPos和YPos对象作为new_xnew_y参数。

首先,在你的导入语句下添加以下代码:

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")


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接