【问题标题】:Multiple tables (5) on one page using ReportLab使用 ReportLab 在一个页面上显示多个表 (5)
【发布时间】:2021-11-27 04:34:16
【问题描述】:

我在 Python 中有以下代码来使用 ReportLab 生成两个表。

有没有办法使用 ReportLab 将这两个表格并排放置?

from reportlab.lib import colors
from reportlab.lib.pagesizes import letter, inch
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle

doc = SimpleDocTemplate("simple_table_grid.pdf", pagesize=letter)
elements = []

data= [['00', '01', '02', '03', '04','10', '11', '12', '13', '14'],
   ['10', '11', '12', '13', '14', '10', '11', '12', '13', '14'],
   ['20', '21', '22', '23', '24', '10', '11', '12', '13', '14'],
   ['30', '31', '32', '33', '34', '10', '11', '12', '13', '14']]
   
t=Table(data,5*[0.3*inch], 4*[0.2*inch])
t.setStyle(TableStyle([
        ('BACKGROUND',(0,0),(4,0),colors.gray),
                   ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
                   ('BOX', (0,0), (-1,-1), 0.25, colors.black),
                   ]))

elements.append(t)

data= [['100', '01', '02', '03', '04'],
   ['10', '11', '12', '13', '14'],
   ['20', '21', '22', '23', '24'],
   ['30', '31', '32', '33', '34']]
   
t=Table(data,5*[0.3*inch], 4*[0.2*inch])
t.setStyle(TableStyle([
        ('BACKGROUND',(0,0),(4,0),colors.gray),
                   ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
                   ('BOX', (0,0), (-1,-1), 0.25, colors.black),
                   ]))

elements.append(t)

doc.build(elements)

【问题讨论】:

  • 你试过用相框吗?您可以将 2 个框架彼此相邻放置,然后调整表格宽度以填充每个框架(因此它会强制 reportlab 在下一个框架中绘制),或者手动附加一个 FrameBreak 以进入下一个框架。如果到那时没有其他人回答,我应该能够在今晚晚些时候制定一个示例。

标签: python python-2.7 reportlab


【解决方案1】:

创建第三个表,作为您创建的两个表的外壳。该表将有两列和一行。每列将是相应子表的大小。

例子:

在不同的表变量中创建两个表(例如 table1table2

t1_w = <your first table width size>
t2_w = <your second table width size>
data = [[table1, table2]]
shell_table = Table(data, colWidths=[t1_w, t2_w])

【讨论】:

    【解决方案2】:

    示例:
    *完整代码

    from reportlab.lib import colors
    from reportlab.lib.pagesizes import letter, inch
    from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
    
    doc = SimpleDocTemplate("simple_table_grid.pdf", pagesize=letter)
    elements = []
    
    data1 = [['00', '01', '02', '03', '04', '10', '11', '12', '13', '14'],
            ['10', '11', '12', '13', '14', '10', '11', '12', '13', '14'],
            ['20', '21', '22', '23', '24', '10', '11', '12', '13', '14'],
            ['30', '31', '32', '33', '34', '10', '11', '12', '13', '14']]
    
    t1 = Table(data1, 5 * [0.3 * inch], 4 * [0.2 * inch])
    t1.setStyle(TableStyle([
        ('BACKGROUND', (0, 0), (4, 0), colors.gray),
        ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
        ('BOX', (0, 0), (-1, -1), 0.25, colors.black),
    ]))
    
    
    data2 = [['100', '01', '02', '03', '04'],
            ['10', '11', '12', '13', '14'],
            ['20', '21', '22', '23', '24'],
            ['30', '31', '32', '33', '34']]
    
    t2 = Table(data2, 5 * [0.4 * inch], 4 * [0.2 * inch])
    t2.setStyle(TableStyle([
        ('BACKGROUND', (0, 0), (4, 0), colors.gray),
        ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
        ('BOX', (0, 0), (-1, -1), 0.25, colors.black),
    ]))
    
    data = [[t1, t2]]
    # adjust the length of tables
    t1_w = 3 * inch
    t2_w = 3 * inch
    shell_table = Table(data, colWidths=[t1_w, t2_w])
    elements.append(shell_table)
    doc.build(elements)
    

    【讨论】:

    • 效果很好,但无法在同一行获取图像和段落
    猜你喜欢
    • 2013-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-14
    相关资源
    最近更新 更多