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

发布于 2025-01-21 12:43:38 字数 1212 浏览 0 评论 0原文

我正在尝试关注此 tutorial ,特别是 tuto 2 em>,但我一直遇到以下错误,我不知道为什么。我复制并粘贴了教程中的代码。我使用2.5.2版。

valueerror:参数“ new_x”(0)的无效值,必须是实例 枚举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")

I'm trying to follow this tutorial, specifically the Tuto 2, but I keep getting the following error and I don't know why. I copied and pasted the code from the tutorial. I'm on version 2.5.2.

ValueError: Invalid value for parameter "new_x" (0),must be instance
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")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉怎暖 2025-01-28 12:43:38

看起来 github project new_xnew_y的XPO和YPO对象。

首先,在您的导入语句下方添加此直接:

来自fpdf.enums导入XPO,YPOS

这将带来您需要正确设置页码位置的课程。

然后更新页脚函数 - 而不是

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,ypos.next,“ c” )

Looks like the github project indicates that you need to be passing in an XPos and YPos object for new_x and new_y.

First, add this right below your import statement:

from fpdf.enums import XPos, YPos.

This will bring in the classes you need to correctly set the position of the page number.

Then update the footer function - instead of

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文