我想使用reportlab打印一个缩进的编号列表。

我想打印什么


  欢迎来到我的清单
  
  
  项目清单
  项目清单
  项目清单
  
  
  谢谢阅读


我的reportlab模板:

from reportlab.pdfgen import canvas
canvas = canvas.Canvas("mypdf.pdf", pagesize=letter)
canvas.drawString(inch, inch, "Hello")
canvas.save()


我无法从reportlab用户指南中获得该示例来使用我的画布。如何将此列表打印到pdf上?

最佳答案

将以下导入添加到脚本的开头应可解决您收到的错误:

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()

关于python - ReportLab中的编号列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48733507/

10-13 07:06