【问题标题】:In Reportlab what data format for Table在Reportlab中表格的数据格式是什么
【发布时间】:2017-12-21 11:30:07
【问题描述】:

在报告中我尝试添加行和列但遇到错误

/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】:

    我认为问题在于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']
    

    【讨论】:

    • 是的,这是真的。有什么解决办法?
    • 尝试在 product_data 周围多加一对括号:product_data = [[ ... ]]。或者,我看到您说您想要消息中的行和列,所以如果您想要多行,那么像网格一样对数据进行排序。
    • 我尝试添加一对括号。但仍然面临错误。就像我在问题中提到的那样
    • 查看 product_data 与 pdf_data。
    • 请在这里写一些代码以便我理解
    猜你喜欢
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多