我试图遵循这个教程,特别是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")发布于 2022-04-16 12:45:37
看起来,github项目表示您需要为new_x和new_y传入一个XPos和YPos对象。
首先,在导入语句下面添加以下内容:
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")。
https://stackoverflow.com/questions/71878703
复制相似问题