gpt4 book ai didi

python - 如何在 Python 中的 Reportlab 表中添加复选框?

转载 作者:行者123 更新时间:2023-12-05 00:48:34 30 4
gpt4 key购买 nike

我正在尝试使用 reportlab 通过 python 生成 PDF。 (初级)

基本上,我想创建一个带有复选框的表格。

例如,引用下面的代码:

    ...
data =[
[Paragraph("Option 1",style=custom_para), "anything"],
[Paragraph("Option 2",style=custom_para), "anything"]
]

t=Table(data, style=style_table, colWidths=[100, 100])
Story.append(t)
...

我已经测试过上面的代码可以正确生成表格。

现在,我想要更进一步的东西,比如:

    ...
data =[
[Paragraph("Option 1",style=custom_para), checkbox_1],
[Paragraph("Option 2",style=custom_para), checkbox_2]
]

t=Table(data, style=style_table, colWidths=[100, 100])
Story.append(t)
...

checkbox_1、checkbox_2应该如何实现?

实现这一目标的最有效方法是什么?

最佳答案

我希望这会有所帮助。我创建了 checkbox_1 和 checkbox_2 作为类的实例:

class InteractiveCheckBox(Flowable):
def __init__(self, text='A Box'):
Flowable.__init__(self)
self.text = text
self.boxsize = 12

def draw(self):
self.canv.saveState()
form = self.canv.acroForm
form.checkbox(checked=False,
buttonStyle='check',
name=self.text,
tooltip=self.text,
relative=True,
size=self.boxsize)
self.canv.restoreState()
return

然后你可以这样做:

...
checkbox_1 = InteractiveCheckBox('cb1')
checkbox_2 = InteractiveCheckBox('cb2')
data =[
[Paragraph("Option 1",style=custom_para), checkbox_1],
[Paragraph("Option 2",style=custom_para), checkbox_2]
]

t=Table(data, style=style_table, colWidths=100])
Story.append(t)
...

关于python - 如何在 Python 中的 Reportlab 表中添加复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49799852/

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