Convert plain text to PDF in Python

2020-02-29 04:30发布

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)

2条回答
欢心
2楼-- · 2020-02-29 05:06

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'])
查看更多
家丑人穷心不美
3楼-- · 2020-02-29 05:15

I had similar issue. 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 same as original text file

查看更多
登录 后发表回答