2012-10-12 16 views
5

Ho il seguente codice in python per generare due tabelle utilizzando ReportLab. C'è un modo per posizionare questi due tavoli affiancati usando ReportLab?Più tabelle (5) una pagina utilizzando 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

Hai provato a usare fr ames? È possibile posizionare 2 fotogrammi uno accanto all'altro e modificare la larghezza del tavolo per riempire ciascun fotogramma (quindi obbliga a reportlab di disegnare nel successivo), o aggiungere manualmente un FrameBreak per passare al successivo. Dovrei essere in grado di elaborare un esempio più tardi, stasera, se nessun altro risponde per allora. – Nitzle

risposta

4

Creare una terza tabella che servirà come il guscio per le due tabelle create. Questa tabella avrà due colonne e una riga. Ogni colonna sarà la dimensione della rispettiva tabella figlio.

Esempio:

Crea il tuo due tabelle nella tabella diversa Vars (ad es 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]) 
2

Esempio:
* Codice intero

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) 
+0

funziona benissimo, ma non è possibile ottenere un'immagine e un paragrafo sulla stessa riga – radtek

Problemi correlati