简体   繁体   中英

python wrap text and reportlab

I have a little code and I would like to wrap my long string in every 10th character and then add it into a PDF using reportlab:

This is how I try:

text = '*long_text_long_text_long_text_long_text*'
text = "\n".join(wrap(text, 10))
canvas.drawString(5,227, text)

My pdf was created but where I want to break the lines I can only see black rectangles. You can see the attached picture:

在此处输入图片说明

Can you help me? Thank you!

drawString draws a single line. so you will need to adjust the coordinate for each line in a loop.

y = 227
for line in wrap(text, 10):
    canvas.drawString(5, y, line)
    y += 15

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM