user2067030
user2067030

Reputation: 764

ReportLab Wrap with drawstring

I am new to report lab and python. I know with reportlab you can wrap test in paragraphs and Tables but I am drawing a report with variable text and sometimes the text is two long and needs to be wrapped. Is there a way to allow the text send to drawstring in reportlab to be wrapped if it is too long ?

System Info: Windows 8 machine, ReportLab 3.3, Python 3

Upvotes: 2

Views: 4312

Answers (1)

user2067030
user2067030

Reputation: 764

It looks like drawstring does not allow for wrapping. I ended up solving the problem by using the textwrap python function; that will split the original string into a list and then take the result of the list and manually creating a new line with drawstring if it passes certain length.

import textwrap



 if len(originalstring) > 45:
        wrap_text = textwrap.wrap(originalstring, width=45)
        canvas.drawString(coordx, coordy, wrap_text[0])
        canvas.drawString(coordx, coordy, wrap_text[1])
  else:
        canvas.drawString(coordx, coordy, originalstring)

Upvotes: 10

Related Questions