Python:如何使Reportlab移动到PDF输出中的下一行

2024-04-26 13:20:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在查询数据库中的数据,将其放入Python中的列表中,然后遍历这些列表,以便将数据打印成PDF格式。{我已经读过了。在

问题:当当前行超出页面右边框时,我无法跳转到新行。我需要一个“回报”

我现在的情况是:

x = 72 #1 inch margin from
y = 720 #10 inch margin from bottom
mypdf = canvas.Canvas(response) #new canvas called 'mypdf'
mypdf.setPageSize(letter) #set size to 8.5x11
list1 = ['item 1.1', 'item 1.2', 'item 1.3',]
list2 = ['item 2.1', 'item 2.2', 'item 2.3',]

detaillist = [ list1, list2 ]

for details in detaillist:
    for line_item in details:
        mypdf.roundRect(x-15, y, 10, 10, 2, stroke=1) #each line item has a check box
        mypdf.drawString(x, y, line_item) #write the value for each line item at x,y coordinate
        y -= 14 #x, y coordinate goes down by 14/72s of an inch
        #if the current location is within bottom 1 inch margin
        if y < 72:
            y = 720 #reset y value
            page = canvas.Canvas(response) #create a new page
            mypdf.showPage() #show the new page
    mypdf.line(x,y,x*7.5,y) #draw a line to separate sections
    y -= 17 #add a buffer below the line

mypdf.save()

现在,这个循环遍历我的两个列表。当内容在一英寸的下边距内时,我会转到新的页面。几乎所有的列表项都能放在页面上。但是,如果我将列表项更改为超长,例如:

^{pr2}$

然后我的文字就从页面的右侧消失了。在

我能做什么? 我很高兴听到你会怎么做。我有一个未知数量的列表要迭代,每个列表有10-20个数据点,每个(当前)打印在一行上。在


Tags: the数据frommargin列表newforline