gpt4 book ai didi

Python 与 ReportLab。如何用 SimpleDocTemplate 写一行?

转载 作者:行者123 更新时间:2023-11-30 23:30:14 28 4
gpt4 key购买 nike

我正在使用 Python 2.7.6 和 Django 1.5.5。如何在 SimpleDocTemplate 中写入一行?

我正在尝试这个:

@login_required
def report(request):
rep = Report(request.user.username + "_cities.pdf")

# Title
rep.add_header("Cities")

line = Line(0, 100, 500, 100)
rep.add(line)

# Body
columns = ("City")
cities = [(p.name) for p in City.objects.all()]

table = Table([columns] + cities, style=GRID_STYLE)
table.hAlign = "LEFT"
table.setStyle([('BACKGROUND', (1, 1), (-2, -2), colors.lightgrey)])

rep.add(table)

rep.build()
return rep.response

Line()from reportlab.graphics.shapes import Line。Report 类只是 SimpleDocTemplate 的包装类:

class Report:
styles = None
response = None
document = None
elements = []

def __init__(self, report_file_name):
self.styles = styles.getSampleStyleSheet()
self.styles.add(ParagraphStyle(name='Title2',
fontName="Helvetica",
fontSize=12,
leading=14,
spaceBefore=12,
spaceAfter=6,
alignment=TA_CENTER),
alias='title2')

self.response = HttpResponse(mimetype="application/pdf")
self.response["Content-Disposition"] = "attachment; filename=" + report_file_name

self.document = SimpleDocTemplate(self.response, topMargin=5, leftMargin=2, rightMargin=1, bottomMargin=1)
self.document.pagesize = portrait(A4)

return

def add_header(self, header_text):
p = Paragraph(header_text, self.styles['Title2'])
self.elements.append(p)

def add(self, paragraph):
self.elements.append(paragraph)

def build(self):
self.document.build(self.elements)

当我调用报告函数时,我收到错误消息:

Line instance has no attribute 'getKeepWithNext'

当我使用 Line() 删除/注释这些行时,不会出现错误。

你能帮我吗?那一行怎么写?

最佳答案

仅将 Line 添加到元素列表是行不通的:您只能将 Flowable 传递给 SimpleDocTemplate.build()。但您可以将其包装在 Drawing 中,它是一个 Flowable:

d = Drawing(100, 1)
d.add(Line(0, 0, 100, 0))
rep.add(d)

关于Python 与 ReportLab。如何用 SimpleDocTemplate 写一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20763291/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com