[reportlab-users] Create a table with data from wxpythongrid

Robin Becker robin at reportlab.com
Thu Dec 11 05:31:12 EST 2008


Barthe Guillaume wrote:

> Hello,

>

> I want to create a table. The data sources are cells from a wxgrid from wxpython. I build my table with this code :

>

> styleSheet = getSampleStyleSheet()

> doc = SimpleDocTemplate(destination_file)

> Story = [Spacer(1, 2*inch)]

> style = styleSheet["Normal"]

> Story.append(Spacer(1, 3*cm))

> data = []

> en_tete = [u'Repère', u'Désignations', u'T ECS [°C]', 'Nombre', u'Débit de base [l / s]', u'Débits ECS [l / s]', u'Débits ECS totaux [l / s]']

> data.append(en_tete)

> for i in range(nb_lignes_ecs_semi_instantanee - 1):

> data.append("[" + self.detail_ecs.GetCellValue(i, 0) + ", " + "\'" + self.detail_ecs.GetCellValue(i, 1) + "\'" + ", " + self.detail_ecs.GetCellValue(i, 2) + ", " + self.detail_ecs.GetCellValue(i, 3) + ", " + self.detail_ecs.GetCellValue(i, 4) + ", " + self.detail_ecs.GetCellValue(i, 5) + ", " + self.detail_ecs.GetCellValue(i, 6) + "]")

> t=Table(data, 7*[2*cm], nb_lignes_ecs_semi_instantanee*[0.9*cm])

> # Ajustement des colonnes une par une en fct du contenu

> t._argW[0]=1.1*cm

> t._argW[1]=3.8*cm

> t._argW[3]=1.4*cm

> t._argW[4]=3*cm

> t._argW[5]=2.6*cm

> t._argW[6]=3.6*cm

> # Style du tableau

> t.setStyle(TableStyle([('BACKGROUND', (0, 0), (-1, 0), colors.green), ('ALIGN', (0, 0), (-1, 0), 'CENTER'), ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'), ('GRID', (0, 0), (-1, -1), 0.5, colors.black), ('ALIGN',(0, 1),(0, -1),'CENTER'), ('ALIGN',(1, 1),(1, -1),'LEFT'), ('ALIGN',(2, 1),(2, -1),'RIGHT'), ('ALIGN',(3, 1),(3, -1),'RIGHT'), ('ALIGN',(4, 1),(4, -1),'RIGHT'), ('ALIGN',(5, 1),(5, -1),'RIGHT'), ('ALIGN',(6, 1),(6, -1),'RIGHT')]))

> Story.append(t)

> # Création du fichier

> doc.build(Story, onFirstPage=self.partie_1_pdf)

>

> But this code return an error :

>

> Traceback (most recent call last):

> File "./astugeclim.py", line 537, in creer_pdf_ecs_semi_instantanee

> t=Table(data, 7*[2*cm], nb_lignes_ecs_semi_instantanee*[0.9*cm])

> File "/usr/lib/python2.5/site-packages/reportlab/platypus/tables.py", line 251, in __init__

> raise ValueError, "%s not enough data points in row %d!" % (self.identity(),i)

> ValueError: <Table at 3045439180 25 rows x 7 cols> with cell(0,0) containing

> 'Rep\xc3\xa8re' not enough data points in row 1!

>

> And I have see that the result of data is :

>

> [[u'Rep\xe8re', u'D\xe9signations', u'T ECS [\xb0C]', 'Nombre', u'D\xe9bit de base [l / s]', u'D\xe9bits ECS [l / s]', u'D\xe9bits ECS totaux [l / s]'], u"[1, 'Paillasse (0.20)', 45, 1, 0.2, 0.14, 0.14]", u"[2, 'Vidoir (0.20)', 45, 2, 0.2, 0.14, 0.28]", u"[3, 'Auge 3 postes (0.60)', 45, 3, 0.6, 0.42, 1.26]", u"[4, 'Vasque (0.20)', 45, 4, 0.2, 0.14, 0.56]", u"[5, 'Evier (0.20)', 45, 5, 0.2, 0.14, 0.7]", u"[6, 'Douche (0.20)', 45, 6, 0.2, 0.14, 0.84]", u"[7, 'Lave-mains (0.10)', 45, 7, 0.1, 0.07, 0.49]", u"[8, 'Timbre d'office (0.20)', 45, 8, 0.2, 0.14, 1.12]", u"[9, 'Lave bassin (0.30)', 45, 9, 0.3, 0.21, 1.89]", u"[10, 'Baignoire 2 (0.40)', 45, 10, 0.4, 0.28, 2.8]", u"[11, 'Baignoire 1 (0.33)', 60, 11, 0.33, 0.33, 3.63]", u"[12, 'Auge 2 postes (0.40)', 45, 12, 0.4, 0.28, 3.36]", u"[13, 'Auge 3 postes (0.60)', 60, 13, 0.6, 0.6, 7.8]", u"[14, 'Evier (0.20)', 60, 14, 0.2, 0.2, 2.8]", u"[15, '', 45, 15, 0.0, 0.0, 0.0]", u"[16, 'Gros debit (0.66)', 60, 16,

> 0.66, 0.66, 10.56]", u"[17, 'Lave-mains (0.10)', 45, 17, 0.1, 0.07, 1.19]", u"[18, 'Auge 2 postes (0.40)', 45, 18, 0.4, 0.28, 5.04]", u"[19, 'Vidoir (0.20)', 45, 19, 0.2, 0.14, 2.66]", u"[20, 'Vidoir (0.20)', 60, 20, 0.2, 0.2, 4.0]", u"[21, 'Lavabo (0.20)', 45, 21, 0.2, 0.14, 2.94]", u"[22, 'Lave-mains (0.10)', 45, 22, 0.1, 0.07, 1.54]", u"[23, 'Evier (0.20)', 45, 23, 0.2, 0.14, 3.22]", u"[24, 'Paillasse (0.20)', 60, 24, 0.2, 0.2, 4.8]"]

>

> And I have try with this data :

>

> data = [en_tete, [1, 'Paillasse (0.20)', 45, 1, 0.2, 0.14, 0.14], [1, 'Paillasse (0.20)', 45, 1, 0.2, 0.14, 0.14], [1, 'Paillasse (0.20)', 45, 1, 0.2, 0.14, 0.14], [1, 'Paillasse (0.20)', 45, 1, 0.2, 0.14, 0.14], [1, 'Paillasse (0.20)', 45, 1, 0.2, 0.14, 0.14], [1, 'Paillasse (0.20)', 45, 1, 0.2, 0.14, 0.14], [1, 'Paillasse (0.20)', 45, 1, 0.2, 0.14, 0.14], [1, 'Paillasse (0.20)', 45, 1, 0.2, 0.14, 0.14], [1, 'Paillasse (0.20)', 45, 1, 0.2, 0.14, 0.14], [1, 'Paillasse (0.20)', 45, 1, 0.2, 0.14, 0.14], [1, 'Paillasse (0.20)', 45, 1, 0.2, 0.14, 0.14], [1, 'Paillasse (0.20)', 45, 1, 0.2, 0.14, 0.14], [1, 'Paillasse (0.20)', 45, 1, 0.2, 0.14, 0.14], [1, 'Paillasse (0.20)', 45, 1, 0.2, 0.14, 0.14], [1, 'Paillasse (0.20)', 45, 1, 0.2, 0.14, 0.14], [1, 'Paillasse (0.20)', 45, 1, 0.2, 0.14, 0.14], [1, 'Paillasse (0.20)', 45, 1, 0.2, 0.14, 0.14], [1, 'Paillasse (0.20)', 45, 1, 0.2, 0.14, 0.14], [1, 'Paillasse (0.20)', 45, 1, 0.2, 0.14, 0.14], [1, 'Paillasse

> (0.20)', 45, 1, 0.2, 0.14, 0.14], [1, 'Paillasse (0.20)', 45, 1, 0.2, 0.14, 0.14], [1, 'Paillasse (0.20)', 45, 1, 0.2, 0.14, 0.14], [1, 'Paillasse (0.20)', 45, 1, 0.2, 0.14, 0.14], [1, 'Paillasse (0.20)', 45, 1, 0.2, 0.14, 0.14]]

>

> And I think that is my data format is bad but I don't see how to transform my data.

>

> Can you help me please ?

>

......

well the first set of data is a bit confused about what is in each row. The
first row contains 7 items, but the next element after that row is actually a
singleton unicode.

In your second attempt things look better (assuming en_tete is the same).

It looks like your problem is actually with the conversion into a list of the
wxgrid rows (because they are strings).

A simple starting solution is to build the row list directly eg

for i in range(nb_lignes_ecs_semi_instantanee - 1):
row =[]
for j in xrange(7): #j=0,1,....6
row.append(self.detail_ecs.GetCellValue(i, j))
data.append(row)


--
Robin Becker


More information about the reportlab-users mailing list