Добавление таблицы с помощью Django и лабораторного отчета
Следующий код работает для создания pdf, то, что я хотел, это поместить данные в таблицу
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')
Я пытался добавить данные в таблицу, то, что я пробовал, выглядит следующим образом
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')
Это печатает одну букву в каждой строке таблицы, как я могу исправить проблему и данные в простой таблице, любая помощь будет высоко оценена.
Проблема в вашем for-петле. Измените его на следующий:
for user in users:
lines.append((user.username, user.email, user.first_name))