【问题标题】:ReportLab rows with different columns具有不同列的 ReportLab 行
【发布时间】:2015-11-18 00:50:16
【问题描述】:

我正在将我的数据写入 pdf 表中,我的第一行有 4 列,第二行有 2 列,第三行有 4 列,依此类推

            table.setStyle(TableStyle([
                   ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
                   ('BOX', (0,0), (-1,-1), 0.25, colors.grey),
                   ]))

这是我用来设置表格样式的代码,但我每行得到 4 列。我想根据我传递的列表的长度生成列,就像我的列表 li [[[1,2 ,3,4][1,2][1,2,3,4][1,2,3]] 在第一行中我需要 4 列,在第二行中需要 2 列等等。我如何使用 tablestyle 来实现它。

【问题讨论】:

  • @Andy Kubiak code 数据 = [[0, 1, 2, 3], [0, 1], [0, 1, 2, 3], [0, 1, 2] ] gridlines = [('GRID', (i, 0), (i, len(j)), colors.grey) for i, j in enumerate(data)] print gridlines style = TableStyle(gridlines) s = getSampleStyleSheet( ) s = s["BodyText"] t=Table(data) t.setStyle(style) 这是我的代码,我在 t.setstyle 中遇到异常

标签: python pdf reportlab


【解决方案1】:

仅显式绘制您需要的边框。在documentation 中查找“TableStyle 行命令”。

编辑:

假设您的数据列表如下所示:

data = [[0, 1, 2, 3],
        [0, 1],
        [0, 1, 2, 3],
        [0, 1, 2]]

那么也许这样的事情会起作用:

>>> lineweight = 0.25
>>> gridlines = [('GRID', (i, 0), (i, len(j)), lineweight, colors.grey) for i, j in enumerate(data)]
>>> gridlines
[('GRID', (0, 0), (0, 4), 0.25, colors.grey),
 ('GRID', (1, 0), (1, 2), 0.25, colors.grey),
 ('GRID', (2, 0), (2, 4), 0.25, colors.grey),
 ('GRID', (3, 0), (3, 3), 0.25, colors.grey)]

希望对您有所帮助。

edit2: 添加lineweight

【讨论】:

  • stackoverflow.com/questions/13812603/… .我也经历过这个解决方案,但我无法理解如何动态创建列。
  • 数据 = [[0, 1, 2, 3], [0, 1], [0, 1, 2, 3], [0, 1, 2]] 网格线 = [(' GRID', (i, 0), (i, len(j)), colors.grey) for i, j in enumerate(data)] print gridlines style = TableStyle(gridlines) s = getSampleStyleSheet() s = s[" BodyText"] t=Table(data) t.setStyle(style)
  • 如果 len(cmd)
  • 糟糕,抱歉,错过了线宽。我将编辑我的解决方案 sn-p。
  • 我已经尝试运行。我们无法从上面的代码中删除列:)。
猜你喜欢
  • 2016-01-28
  • 2014-12-08
  • 1970-01-01
  • 1970-01-01
  • 2011-05-08
  • 2019-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多