在Reportlab中用于Table的数据格式

问题描述 投票:-1回答:1

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

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

    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)
django python-2.7 reportlab pypdf pypdf2
1个回答
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']
© www.soinside.com 2019 - 2024. All rights reserved.