首页 > 解决方案 > 导入多个模块并从模块中导入类

问题描述

我正在尝试使用 pyFPDF 使用 python 制作 pdf,我希望日期是自动的,并且正在尝试将 datetime 模块与 fpdf 模块一起使用,但我遇到了错误。

尝试 import datetime, fpdf 然后, from fpdf import FPDF get error。

然后也导入日期时间,再次从 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()

标签: python-3.x

解决方案


这不是模块的问题: 的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'

推荐阅读