7

ReportLab を使用して 2 つのテーブルを生成する Python の次のコードがあります。

ReportLab を使用してこれら 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 = []

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)
4

2 に答える 2

5

Create a third table that will serve as the shell for the two tables you created. This table will have two columns and one row. Each column will be the size of the respective child table.

Example:

create your two tables in different table vars (e.g. table1, table2)

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])
于 2012-12-15T18:28:57.567 に答える
3

例:
*コード全体

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-06-01T08:32:48.877 に答える