Search code examples
pythonreportlab

How to resume \n read from excel file in output pdf file - reportlab python


I am trying to read from excel file and generate the output file using reportlab library in python. Now the text in excel file is as below :-

enter image description here

When I am reading it into pdf I am not able to resume newlines.

enter image description here

Below is the code I am using for the same :-

for body_column in body_columns:
    body_para = Paragraph(f'{body_column}', style['BodyText'])
    details.append(body_para)
                
doc.multiBuild(details)

Could anyone please help me with this?


Solution

  • When you're using the paragraph with reportlab, it treats your text like HTML. This means that it doesn't interpret \n as a new line. Instead, you could replace the '\n' with '< br/ >' (without the spaces), as such:

    for body_column in body_columns:
       body_column = body_column.replace('\n', '<br/>')
       body_para = Paragraph(f'{body_column}', style['BodyText'])
       details.append(body_para)