fpdf2错误的参数无效值“ new_x”
我正在尝试关注此 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来 github project
new_x
和new_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
andnew_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 ofself.cell(0, 10, f"Page {self.page_no()}/{{nb}}", 0, 0, "C")
, useself.cell(0, 10, f"Page {self.page_no()}/{{nb}}", XPos.RIGHT, new_y=YPos.NEXT, "C")
.