python-3.x - 导入多个模块并从模块中导入类
问题描述
我正在尝试使用 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()
解决方案
这不是模块的问题: 的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'
推荐阅读
- api - VueJS - 如何在同一组件中的另一个 API 调用中使用一个 API 调用的响应
- java-native-interface - JNI 类和接口命名
- ansible - Ansible with_items 第二个列表作为条件
- c# - OneTimeSetUp:System.ComponentModel.Win32Exception:没有足够的内存资源来处理此命令
- raspberry-pi - 尝试在 Raspberry Pi 集群上设置多语言环境教堂
- c++ - 从类/结构中声明的枚举类型到在该类/结构中也声明的对等静态方法?
- python - 获取附加到Django中每个对象的前N个标签
- vue.js - 在 Nuxt js 项目 axios 自动编码 url
- java - 从具有列表字段的 Java 对象创建 QueryDSL 谓词的问题
- arrays - C:如何将输入作为未知大小的矩阵