使用 Django 和报告实验室添加 table
adding table with Django and report lab
以下代码适用于 pdf 创建,我想要的是将数据放入 table
def pdf_view(request):
enc = pdfencrypt.StandardEncryption("pass", canPrint=0)
buf = io.BytesIO()
c = canvas.Canvas(buf, encrypt=enc)
width, height = A4
textob = c.beginText()
textob.setTextOrigin(inch, inch)
textob.setFont("Helvetica", 14)
lines = []
users = User.objects.filter(is_staff=False)
for user in users:
lines.append(user.username)
lines.append(user.email)
lines.append(user.first_name)
for line in lines:
textob.textLine(line)
c.drawText(textob)
c.showPage()
c.save()
buf.seek(0)
return FileResponse(buf, as_attachment=True, filename='users.pdf')
我尝试将数据附加到一个table,我尝试的结果如下
def pdf_view(request):
enc = pdfencrypt.StandardEncryption("pass", canPrint=0)
buf = io.BytesIO()
c = canvas.Canvas(buf, encrypt=enc)
width, height = A4
textob = c.beginText()
textob.setTextOrigin(inch, inch)
textob.setFont("Helvetica", 14)
lines = []
users = User.objects.filter(is_staff=False)
for user in users:
lines.append(user.username)
lines.append(user.email)
lines.append(user.first_name)
table = Table(lines, colWidths=10 * mm)
table.setStyle([("VALIGN", (0, 0), (-1, -1), "MIDDLE"),
("ALIGN", (0, 0), (-1, -1), "CENTER"),
('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black)])
table.wrapOn(c, width, height)
table.drawOn(c, 0 * mm, 5 * mm)
styles = getSampleStyleSheet()
ptext = "This is an example."
p = Paragraph(ptext, style=styles["Normal"])
p.wrapOn(c, 50 * mm, 50 * mm) # size of 'textbox' for linebreaks etc.
p.drawOn(c, 0 * mm, 0 * mm) # position of text / where to draw
c.save()
buf.seek(0)
return FileResponse(buf, as_attachment=True, filename='users.pdf')
这是在 table 的每一行上打印单个字母,我如何解决问题并将数据简化为 table,非常感谢任何帮助。
问题出在你的 for 循环中。将其更改为:
for user in users:
lines.append((user.username, user.email, user.first_name))
以下代码适用于 pdf 创建,我想要的是将数据放入 table
def pdf_view(request):
enc = pdfencrypt.StandardEncryption("pass", canPrint=0)
buf = io.BytesIO()
c = canvas.Canvas(buf, encrypt=enc)
width, height = A4
textob = c.beginText()
textob.setTextOrigin(inch, inch)
textob.setFont("Helvetica", 14)
lines = []
users = User.objects.filter(is_staff=False)
for user in users:
lines.append(user.username)
lines.append(user.email)
lines.append(user.first_name)
for line in lines:
textob.textLine(line)
c.drawText(textob)
c.showPage()
c.save()
buf.seek(0)
return FileResponse(buf, as_attachment=True, filename='users.pdf')
我尝试将数据附加到一个table,我尝试的结果如下
def pdf_view(request):
enc = pdfencrypt.StandardEncryption("pass", canPrint=0)
buf = io.BytesIO()
c = canvas.Canvas(buf, encrypt=enc)
width, height = A4
textob = c.beginText()
textob.setTextOrigin(inch, inch)
textob.setFont("Helvetica", 14)
lines = []
users = User.objects.filter(is_staff=False)
for user in users:
lines.append(user.username)
lines.append(user.email)
lines.append(user.first_name)
table = Table(lines, colWidths=10 * mm)
table.setStyle([("VALIGN", (0, 0), (-1, -1), "MIDDLE"),
("ALIGN", (0, 0), (-1, -1), "CENTER"),
('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black)])
table.wrapOn(c, width, height)
table.drawOn(c, 0 * mm, 5 * mm)
styles = getSampleStyleSheet()
ptext = "This is an example."
p = Paragraph(ptext, style=styles["Normal"])
p.wrapOn(c, 50 * mm, 50 * mm) # size of 'textbox' for linebreaks etc.
p.drawOn(c, 0 * mm, 0 * mm) # position of text / where to draw
c.save()
buf.seek(0)
return FileResponse(buf, as_attachment=True, filename='users.pdf')
这是在 table 的每一行上打印单个字母,我如何解决问题并将数据简化为 table,非常感谢任何帮助。
问题出在你的 for 循环中。将其更改为:
for user in users:
lines.append((user.username, user.email, user.first_name))