Convert plain text to PDF in Python

user1327390 picture user1327390 · Apr 11, 2012 · Viewed 22.2k times · Source

For my project, I get a plain text file (report.txt) from another program. It is all formatted in plain text. If you open it in Notepad, it looks nice (as much as a plain text file can). When I open the file in Word and show the paragraphs, I see the ... for spaces and the backwards 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 trouble converting the report.txt to pdf. I have ReportLab, and am able to read the file and make a few changes (like change the text to Courier), but the spacing gets lost. When the file gets read, it appears to strip any extra spaces.

Questions: a) is there an easier way to convert the 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 I'm missing from my paragraph style that will keep the original look?

Here's my code:

# ------------------------------------
# Styles
# ------------------------------------

styleSheet = getSampleStyleSheet()
mystyle = ParagraphStyle(name='normal',fontName='Courier',
                         fontSize=10, 
                         alignment=TA_JUSTIFY, 
                         leading=1.2*12,
                         parent=styleSheet['Normal'])       

#=====================================================================================       
model_report = 'report.txt'

# Create document for writing to pdf  
doc = SimpleDocTemplate(str(pdfPath),  \
                        rightMargin=40, leftMargin=40, \
                        topMargin=40, bottomMargin=25, \
                        pageSize=A4)
doc.pagesize = portrait(A4)

# Container for 'Flowable' objects
elements = []    

# Open the model report
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)

Answer

alexis picture alexis · Apr 12, 2012

ReportLab is the usual recommendation-- as you can see from the "Related" questions on the right side of this page.

Have you tried creating text with just StyleSheet['Normal']? I.e., if you get proper-looking output with the following, the problem is somehow with your style.

Paragraph(para1, style=StyleSheet['Normal'])