[reportlab-users] Setting Paragraphs as individual elements in a data table

Tim Roberts timr at probo.com
Tue Apr 10 18:33:43 EDT 2012


Funke, Matt wrote:

> I've populated matrices for a data table and a style table that I want to pass on to reportlab.platypus.tables.Table(). Here are the matrices as they currently exist:

> ...

> headerTableData:

> [[[<reportlab.platypus.flowables.Image instance at 0x0325B580>], ''],

> [[<reportlab.platypus.flowables.Image instance at 0x0325B580>], ''],

> [[<reportlab.platypus.flowables.Image instance at 0x0325B580>], '']]

>

> However, when I issue this command:

>

> headerTableData[0][1] = 'd'

>

> ... and then look at the contents of headerTableData, I get this:

>

> [[[<reportlab.platypus.flowables.Image instance at 0x0325B580>], 'd'],

> [[<reportlab.platypus.flowables.Image instance at 0x0325B580>], 'd'],

> [[<reportlab.platypus.flowables.Image instance at 0x0325B580>], 'd']]

>

> Why is this? Shouldn't only the top-left corner of the matrix be set to 'd'? Is there a way I can keep from duplicating this data in every row in the column 1? (Does it have something to do with how I set SPAN in column 0?)


No, this is almost certainly caused by the way you created the table
data. I suspect you've fallen into one of the classic Python blunders.
I'm guessing you did something like this:

image = [[Image(...)],'']
headerTableData = [image,image,image]
# or
headerTableData = [image]*3

That would result in the behavior you see. This does not create three
different lists. That creates three references to the SAME list. So:
headerTableData[0][1] = 'd'
changes the one list that all three entries refer to.

If that's what happened, you need to do something to do a deeper copy, like:
headerTableData = [image[:], image[:]. image[:]]

--
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the reportlab-users mailing list