我想使用reportlab打印缩进的编号列表。
我要打印什么
欢迎来到我的清单
- 列表项
- 列表项
- 列表项
醇>感谢您阅读
我的reportlab模板:
from reportlab.pdfgen import canvas
canvas = canvas.Canvas("mypdf.pdf", pagesize=letter)
canvas.drawString(inch, inch, "Hello")
canvas.save()
我无法从reportlab用户指南中获取使用我的画布的示例。如何将此列表打印到我的pdf上?
答案 0 :(得分:3)
将以下导入添加到脚本的开头应解决您收到的错误:
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import inch
此外,如下所示设置 bottomup 参数将启动页面右上角的文本。
canvas = canvas.Canvas("mypdf.pdf", pagesize=letter, bottomup=0)
以下示例应如您所述创建PDF:
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import inch
canvas = canvas.Canvas("mypdf.pdf", pagesize=letter, bottomup=0)
items = [
"List item one",
"List item two",
"List item three",
]
text_object = canvas.beginText(inch, inch)
text_object.textLine("Welcome to my list")
for index, item in enumerate(items):
text_object.textLine(" {}. {}".format(index + 1, item))
text_object.textLine("Thanks or reading")
canvas.drawText(text_object)
canvas.save()