我正在尝试使用pyFPDF用python创建pdf,我希望日期是自动的,并且正在尝试将datetime模块与fpdf模块一起使用,但是出现错误。
尝试导入日期时间,fpdf然后,从fpdf导入FPDF得到错误。
还要导入日期时间,然后再次从fpdf导入FPDF错误,
import datetime
today = datetime.date.today()
yesterday = today - datetime.timedelta(days= 1)
tomorrow = today + datetime.timedelta(days= 1)
from fpdf import FPDF
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
pdf.cell(200, 10, txt=today, ln=1, align="C")
pdf.output("simple_demo_test88 .pdf")
我希望今天的日期显示在pdf中,
但是出现了一个长错误,结尾是:TypeError:类型为'datetime.date'的对象没有len()
这不是模块的问题:的txt
参数cell
可能需要一个str
参数。尝试通过str(today)
:
pdf.cell(200, 10, txt=str(today), ln=1, align="C")
>>> import datetime
>>> datetime.date.today()
datetime.date(2019, 7, 3)
>>> str(datetime.date.today())
'2019-07-03'
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句