Convert plain text to PDF in Python

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:

# ------------------------------------
# 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)
+5
source share
3 answers

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

Have you tried creating text with StyleSheet['Normal']? Ie, if you get the correct output with the following, the problem is somehow related to your style.

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

I had a similar problem. I solved with this code:

from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from reportlab.lib.utils import ImageReader
from PIL import Image

# .....
# ..... some exta code unimportant for this issue....
# ....


# here it is
ptr = open("tafAlternos.txt", "r")  # text file I need to convert
lineas = ptr.readlines()
ptr.close()
i = 750
numeroLinea = 0

while numeroLinea < len(lineas):
    if numeroLinea - len(lineas) < 60: # I'm gonna write every 60 lines because I need it like that
        i=750
        for linea in lineas[numeroLinea:numeroLinea+60]:      
            canvas.drawString(15, i, linea.strip())
            numeroLinea += 1
            i -= 12
        canvas.showPage()
    else:
        i = 750
        for linea in lineas[numeroLinea:]:
           canvas.drawString(15, i, linea.strip())
           numeroLinea += 1
           i -= 12
        canvas.showPage()

Pdf looks exactly like the original text file

0
source

AttributeError: 'reportlab.pdfgen.canvas' 'drawString'

0

All Articles