使用reportlab创建具有动态行和列的表时出现问题

时间:2019-04-08 14:52:41

标签: reportlab

我正在使用reportlab为行和列创建动态代码,以便用户可以通过输入提供行和列的信息。现在我的问题是我成功创建了动态列标题,但是行没有根据列数拆分。它们以pdf格式打印为一行,而不会根据列标题拆分。有谁知道如何解决这一问题?

from reportlab.lib import colors

from reportlab.lib.pagesizes import letter

from reportlab.platypus import SimpleDocTemplate, Table, TableStyle

import os

doc = SimpleDocTemplate("teda.pdf", pagesize=letter)

# container for the 'Flowable' objects
elements = []

# These are the column widths for the headings on the table
m = int(input("enter the number of column headings: "))

row_array = []

for i in range(0,m):

    columnHeading = input("enter col heading")
    row_array.append(columnHeading)
    tableHeading = [row_array]
    tH = Table(tableHeading)          
elements.append(tH)


#generating rows for each column

n = int(input("enter the number of rows: "))

row_array = []

# Assemble rows of data for each column

for i in range(0,m):
    for j in range(0,n):
        columnData = str(input("enter col data"))
        row_array.append(columnData)
        tableRow = [row_array]
        tR=Table(tableRow)
elements.append(tR)

doc.build(elements)

print("writing")

xx = "teda.pdf"
os.startfile(xx)

我希望结果如下:

TOM   CHRIST JERRY VIRAT  MARK
2300  435.5   398  2145  543.34
21      32    20    19    26       AND SO ON....

1 个答案:

答案 0 :(得分:1)

更改此

for i in range(0,m):

    columnHeading = input("enter col heading")
    row_array.append(columnHeading)
    tableHeading = [row_array]
    tH = Table(tableHeading)

for i in range(0,m):
    columnHeading = input("enter col heading")
    row_array.append([columnHeading]) # Note [ ]
tH = Table(row_array)