在 Python 中将纯文本转换为 PDF [英] Convert plain text to PDF in Python

查看:75
本文介绍了在 Python 中将纯文本转换为 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的项目,我从另一个程序获得了一个纯文本文件 (report.txt).它全部采用纯文本格式.如果您在记事本中打开它,它看起来不错(就像纯文本文件一样).当我在 Word 中打开文件并显示段落时,我看到 ... 表示空格,向后 P 表示段落.

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.

我需要将此文件转换为 PDF 并添加一些其他 PDF 页面以制作最终的 PDF.所有这些都发生在 Python 中.

I need to convert this file to PDF and add some other PDF pages to make one final PDF. All this happens in Python.

我在将 report.txt 转换为 pdf 时遇到问题.我有 ReportLab,并且能够读取文件并进行一些更改(例如将文本更改为 Courier),但是间距丢失了.当文件被读取时,它似乎去除了任何额外的空格.

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.

问题:a) 有没有更简单的方法将 report.txt 转换为 pdf?b) 如果没有,有没有办法在我阅读文件时保留我的空格?c) 或者我的段落样式中是否缺少一个可以保持原始外观的参数?

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?

这是我的代码:

# ------------------------------------
# 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)

推荐答案

为了将文本或文本文件转换为 pdf,模块 fpdf 应使用 pip install fpdf命令行界面安装.运行下面的代码,你会在文件夹中找到pdf文件-

For converting text or text file into pdf, module fpdf shall be installed using pip install fpdf in command-line Interface. run the below code and you will find the pdf file in folder-

from fpdf import FPDF 
pdf = FPDF()      
# Add a page 
pdf.add_page()  
# set style and size of font  
# that you want in the pdf 
pdf.set_font("Arial", size = 15)
# open the text file in read mode 
f = open("path where text file is stored\\File_name.txt", "r") 
# insert the texts in pdf 
for x in f: 
    pdf.cell(50,5, txt = x, ln = 1, align = 'C') 
# save the pdf with name .pdf 
pdf.output("path where you want to store pdf file\\File_name.pdf")

参考:https://www.geeksforgeeks.org/convert-text-and-text-file-to-pdf-using-python/

这篇关于在 Python 中将纯文本转换为 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆