Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 710
user2067030 Avatar asked Sep 04 '26 08:09

user2067030


1 Answers

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)
like image 114
user2067030 Avatar answered Sep 07 '26 00:09

user2067030



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!