I have a numpy array that is like this :
[['aaq', '56.18%', 'CC']
['jasl', '0.00%', 'JO']
['arw', '1.74%', 'AS']
['vcf', '1.07%', 'DA']
['hsw', '0.00%', 'LO']
['wey', '41.01%', 'IS']
['fol', '0.00%', 'MO']
['aaw', '0.00%', 'AP'],
['sta', '0.00%', 'TT']]
that I would like to use as the data of a table. In order to do so what I have tried is:
doc = SimpleDocTemplate("sectors.pdf",pagesize=A4)
elements = []
data= [['aaq', '56.18%', 'CC']
['jasl', '0.00%', 'JO']
['arw', '1.74%', 'AS']
['vcf', '1.07%', 'DA']
['hsw', '0.00%', 'LO']
['wey', '41.01%', 'IS']
['fol', '0.00%', 'MO']
['aaw', '0.00%', 'AP'],
['sta', '0.00%', 'TT']]
t=Table(data)
elements.append(t)
doc.build(elements)
When I run the code I recieve the following error:
Traceback (most recent call last):
File "sector_alloc.py", line 236, in <module>
t=Table(data)
File "reportlab/platypus/tables.py", line 212, in __init__
raise ValueError("%s invalid data type" % self.identity())
ValueError: <Table@0x7F61F6D2ACC0 unknown rows x unknown cols>...
invalid data type
It might have to do with converting the values of the np.array
to Paragraphs
? (maybe because they are str
in the data)
In that case what I did was take each column of the np.array
separately (list
) and did :
for valor in list:
list.append(Paragraph('<b>'+valor+'</b>',styleSheet["BodyText"]))
And then try to concat again the lists
to form again the numpy array but didnt worked either ..
Can anybody please help me asign the numpy array as the data for my table?
You appear to be missing commas after most lists in your data list. The list of lists should look like this:
data = [
['aaq', '56.18%', 'CC'],
['jasl', '0.00%', 'JO'],
['arw', '1.74%', 'AS'],
['vcf', '1.07%', 'DA'],
['hsw', '0.00%', 'LO'],
['wey', '41.01%', 'IS'],
['fol', '0.00%', 'MO'],
['aaw', '0.00%', 'AP'],
['sta', '0.00%', 'TT'],
]
Here is a full example that I tested:
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Table
doc = SimpleDocTemplate("example.pdf", pagesize=letter)
data = [
['aaq', '56.18%', 'CC'],
['jasl', '0.00%', 'JO'],
['arw', '1.74%', 'AS'],
['vcf', '1.07%', 'DA'],
['hsw', '0.00%', 'LO'],
['wey', '41.01%', 'IS'],
['fol', '0.00%', 'MO'],
['aaw', '0.00%', 'AP'],
['sta', '0.00%', 'TT'],
]
doc.build([Table(data)])