Reputation: 13
I am creating dynamic code for rows and columns using reportlab so that the user can give info for rows and columns through inputs. Now my problem is I created dynamic column headings successfully but the rows are not splitting according to the number of columns. They are printed on pdf as a line without splitting according to column headings. Does anyone know how to fix this?
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)
I expect the results like:
TOM CHRIST JERRY VIRAT MARK
2300 435.5 398 2145 543.34
21 32 20 19 26 AND SO ON....
Upvotes: 1
Views: 1287
Reputation: 95
Change this
for i in range(0,m):
columnHeading = input("enter col heading")
row_array.append(columnHeading)
tableHeading = [row_array]
tH = Table(tableHeading)
to
for i in range(0,m):
columnHeading = input("enter col heading")
row_array.append([columnHeading]) # Note [ ]
tH = Table(row_array)
Upvotes: 2