Word wrap on report lab PDF table

Pedro Ghilardi picture Pedro Ghilardi · Jan 23, 2010 · Viewed 18.5k times · Source

I'm using the Table of Report Lab library to print a table on a PDF report. I would like to know if it's possible to configure the table to perform an automatic wrapping of the content of a cell.

For example, I have a text that doesn't fit on a cell inside a column. I would like that the table performs the wrap automatically adjusting the content of the cells to fit on the columns width. Is it possible?

Answer

David Raznick picture David Raznick · Jan 23, 2010

You can put any flowable in a table element. It is probably good practice to have all table elements as flowables, so they can be styled the same. For your case you will most likely need a Paragraph flowable. eg.

styles = getSampleStyleSheet()
text = Paragraph("long line",
              styles['Normal'])

You can put `text' into the data you feed to a table and it will automatically wrap.