如何在python中为Tablelab添加一个复选框?

时间:2018-04-12 14:59:56

标签: python reportlab

我正在尝试使用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?

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

2 个答案:

答案 0 :(得分:1)

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

let imageData = imageFile.toString('base64')
let data = Buffer.from(imageData, 'base64');

var post_options = {
      host: '<domain>',
      path: '<path>',
      method: 'POST',
      body: data,
      headers: {
          'Content-Type': 'image/jpeg',
          'Content-Length': Buffer.byteLength(data)
      }
  };

  // Set up the request
  var post_req = http.request(post_options, function(res) {
    // handle response
  });

然后您可以执行以下操作:

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

答案 1 :(得分:0)

我找不到完美的解决方案。最后,我通过在表格中绘制一个矩形来实现类似的结果。矩形用以下代码实现:

class flowable_rect(Flowable):
    def __init__(self, width, height):
        Flowable.__init__(self)
        self.width = width
        self.height = height

    def draw(self):
        self.canv.rect(0, 0, self.width, self.height, fill=0)

因此,它可以直接调用,如:

rect = flowable_rect(6, 6)
t_opt_1=Table([[rect,option_1]], style=style_table, 
             colWidths=[100, 200], hAlign="LEFT")
相关问题