简体   繁体   English

在reportlab中生成的pdf报告与原始文本数据不同

[英]Generated pdf report in reportlab is not similar to original text data

I am trying to generate a pdf document from a text file, but the out does not match the records on the textfile. 我正在尝试从文本文件生成pdf文档,但输出与文本文件上的记录不匹配。

The pdf output data is starting from bottom of page rather than the top. pdf输出数据从页面底部而不是顶部开始。

Please anyone have some ideas 请任何人有一些想法

below is the code: 下面是代码:

Thanks in advance. 提前致谢。

from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
from reportlab.lib.colors import magenta, red

file = open("Computingdata.txt", "r")  # text file I need to convert
    lines = file.read()
file.close()

report = canvas.Canvas('mypdf4.pdf')#new pdf report i am creating
report.setFont("Times-Roman", 20)
report.setFillColor(red)
report.drawCentredString(150, 2.5*inch, "Student details")

report.setFillColor(magenta)
size = 12
y = 2.0*inch
#x = 1.3*inch
for line in lines.split(';'):
    report.setFont("Helvetica", size)
    report.drawString(30,y, line)
    y = y-size*1.2
    #size = size+0.5
report.save() 

Just to add, the output comes in this form of ID 只需添加,输出就是这种形式的ID
USERID 用户身份
LOGIN-NAME 登录名
PASSWORD 密码
SURNAME
NAME 名称
AGE: 年龄:

Instead of ID USERID LOGIN-NAME PASSWORD SURNAME NAME AGE. 代替ID USERID LOGIN-NAME PASSWORD SURNAME NAME AGE。

Thanks everyone, I have been able to solve all the problems i experience with the adjusted code below: 谢谢大家,我已经能够通过以下调整后的代码解决我遇到的所有问题:

from reportlab.pdfgen import canvas
#from reportlab.lib.units import inch
from reportlab.lib.colors import magenta, red

file = open(""yourdata"", "r")  # text file I need to convert
lines = file.read()
file.close()
report = canvas.Canvas('mypdf5.pdf')#new pdf report i am creating
report.setFont("Times-Roman", 20)
report.setFillColor(red)
report.drawCentredString(100, 800, "Student details")

report.setFillColor(magenta)
size = 12
y = 790
#y = 2.0*inch
#x = 1.3*inch
for line in lines.split('\n'):
report.setFont("Helvetica", size)
    report.drawString(10, y, line)
    #y = y-size*1.2
    #size = size+0.5
    y = y - 10
report.save() 

EDIT: In the case whereby your input text file has so many delimiters, then you should do something like this 编辑:在您的输入文本文件具有如此多的分隔符的情况下,那么您应该这样做

import re

file = open("yourdata", "r")  # text file I need to convert
lines = file.read()
lines2 = re.split('; |\n', lines)
file.close()

With the re package, you can include all delimiters to your text file making easy to display the exact content on the reportlab generated pdf file 使用re软件包,您可以将所有定界符包括到文本文件中,从而易于在reportlab生成的pdf文件中显示确切的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM