在Reportlab中用于Table的数据格式

时间:2017-12-21 11:30:08

标签: django python-2.7 reportlab pypdf pypdf2

在报告中我尝试添加行和列但面临错误

   / billing / invioce_report /' int'对象不可迭代

    product_data = [
                str(bill.creation_date), bill.bill_number, bill.staff.user.first_name + " " + bill.staff.user.last_name,
                 products_in_bill.product_qty,
                 str(products_in_bill), products_in_bill.product.reference_number, products_in_bill.product.cost,
                 products_in_bill.discounted_price, products_in_bill.product.cost,
                 customer_name,
                 name,
                 comm_amount,
                 payment_type
                 ]
            print 123
            tp = Table(product_data, 13 * [1.25 * inch], len(product_data) * [0.25 * inch])
            tp.setStyle(TableStyle([('ALIGN', (1, 1), (-2, -2), 'RIGHT'),
                                   ('VALIGN', (0, 0), (0, -1), 'TOP'),
                                   ('ALIGN', (0, -1), (-1, -1), 'CENTER'),
                                   ('VALIGN', (0, -1), (-1, -1), 'MIDDLE'),
                                   ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
                                   ('BOX', (0, 0), (-1, -1), 0.25, colors.black),
                                   ]))

            pdf_data.append(tp)
            all_data.append(tp)

1 个答案:

答案 0 :(得分:0)

我认为问题是reportlab.platypus.Table对象的数据参数需要是列表列表,而不是简单列表。该嵌套列表是二维数据网格,即与表格相同的结构。这在open source docs,关于表方法的第7.1章中有描述。

# So this will work:
data = [
    [1,2,3],
    [4,5,6],
    [7,8,9],
]

# as will a single row:
data = [ [1,2,3] ]

# but a simple list with numbers in it will not work (your current data):
data = [1,2,3]

# however, a list of strings appears to work - like table headings?
data = ['testing', '1', '2']