我正在尝试从文本文件生成pdf文档,但out与文本文件中的记录不匹配。
pdf输出数据从页面底部开始,而不是从顶部开始。
请有人有一些想法
下面是代码:
提前致谢。
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()
只是添加,输出以这种形式出现
ID
USERID
登录名
PASSWORD
SURNAME
NAME
AGE:
答案 0 :(得分:0)
谢谢大家,我已经能够通过以下调整后的代码解决我遇到的所有问题:
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()
编辑: 如果您的输入文本文件有这么多分隔符,那么您应该执行类似这样的操作
import re
file = open("yourdata", "r") # text file I need to convert
lines = file.read()
lines2 = re.split('; |\n', lines)
file.close()
使用re包,您可以在文本文件中包含所有分隔符,以便在reportlab生成的pdf文件中轻松显示确切内容