gpt4 book ai didi

python - 如何使用 Report Lab 在 python 中的 Canvas 上绘制表格

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

我有一个表,我想在 python 的 Canvas 上显示,我已经在 Canvas 中显示了文本,并且我正在返回缓冲区以在另一个函数中返回一个新的 FileResponse。我的代码:

     def Report(dict):
from reportlab.lib.utils import ImageReader
buffer = io.BytesIO()
p = canvas.Canvas(buffer)
textobject = p.beginText()
textobject.setTextOrigin(200, 680)
textobject.textLine('Title')
p.drawText(textobject)
logo = ImageReader('static/img/logo.png')
p.drawImage(logo, 100, 700,width = 400,height=100,mask = None)
data = [['00', '01', '02', '03', '04'],
['10', '11', '12', '13', '14'],
['20', '21', '22', '23', '24'],
['30', '31', '32', '33', '34']]
f = Table(data)
f.setStyle(TableStyle([('BACKGROUND', (1, 1), (-2, -2),
colors.green),
('TEXTCOLOR', (0, 0), (1, -1), colors.red)]))

p.showPage()
p.save()

buffer.seek(0)
return buffer

最佳答案

Table继承了Flowable,其中有一个名为drawOn的方法。

width = 400
height = 100
x = 100
y = 800
f = Table(data)
f.wrapOn(p, width, height)
f.drawOn(p, x, y)

希望这有帮助。

关于python - 如何使用 Report Lab 在 python 中的 Canvas 上绘制表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53529173/

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