In my project, I get a text file (report.txt) from another program. Everything is formatted as plain text. If you open it in Notepad, it looks beautiful (as much as a regular text file can be). When I open the file in Word and show the paragraphs, I see ... for spaces and back P for pararaph.
I need to convert this file to PDF and add some other PDF pages to make one final PDF. All this happens in Python.
I am having problems converting report.txt to pdf. I have ReportLab, and I can read the file and make some changes (for example, change the text to Courier), but the space is lost. When a file is read, it seems to strip away any extra spaces.
Questions: a) Is there an easier way to convert report.txt to pdf? b) If not, is there a way to keep my spaces when I read the file? c) Or is there a parameter that I am missing in my paragraph style that will retain the original look?
Here is my code:
styleSheet = getSampleStyleSheet()
mystyle = ParagraphStyle(name='normal',fontName='Courier',
fontSize=10,
alignment=TA_JUSTIFY,
leading=1.2*12,
parent=styleSheet['Normal'])
model_report = 'report.txt'
doc = SimpleDocTemplate(str(pdfPath), \
rightMargin=40, leftMargin=40, \
topMargin=40, bottomMargin=25, \
pageSize=A4)
doc.pagesize = portrait(A4)
elements = []
infile = file(model_report).read()
report_paragraphs = infile.split("\n")
for para in report_paragraphs:
para1 = '<font face="Courier" >%s</font>' % para
elements.append(Paragraph(para1, style=mystyle))
doc.build(elements)
source
share