Tiarles/reportlabAPI

Recreate the table reconstruction

Opened this issue · 1 comments

Turns the DataFrame into a matrix-like and just put into the Table Reportlab Structure

# -*- coding: utf-8 -*-
"""
Created on Thu Oct 17 11:14:15 2019

@author: tiarl
"""

from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import inch
import numpy as np
import pandas as pd
styles = getSampleStyleSheet()
doc = SimpleDocTemplate("simple_table.pdf", pagesize=letter)
elements = []

###
max_thdh = {3: 4, 5: 4, 7: 4, 9: 4, 11: 2, 13: 2, 15: 2, 17: 1.5, 19: 1.5,
            21: 1.5, 23: 0.6, 25: 0.6, 27: 0.6, 29: 0.6, 31: 0.6}

columns = [3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25]
indexes = ['0, 0°', '0, 45°', '5, 0°', '5, 45°', '10, 0°', '10, 45°',
           '15, 0°', '15, 45°']
values = [[0.01,  0.2, 0.15, 0.01, 0.09, 0.08, 0.01, 0.06, 0.05, 0.01, 0.04, 0.04],
          [ 0.0,  0.2, 0.15, 0.01, 0.09, 0.08, 0.01, 0.06, 0.05, 0.01, 0.05, 0.04],
          [ 0.0, 1.05, 1.39, 0.0, 1.8, 1.5, 0.0, 0.07, 0.05, 0.01, 0.05, 0.05],
          [0.01,  0.9, 1.48, 0.0, 1.85, 1.47, 0.01, 0.06, 0.05, 0.02, 0.04, 0.04],
          [0.01, 2.09, 2.73, 0.0, 3.56, 3.05, 0.01, 0.07, 0.04, 0.0, 0.04, 0.05],
          [0.01, 1.95, 2.83, 0.0, 3.61, 3.02, 0.0, 0.07, 0.05, 0.02, 0.06, 0.07],
          [0.01, 3.15, 4.09, 0.01, 5.32, 4.61, 0.01, 0.07, 0.06, 0.02, 0.06, 0.05],
          [ 0.0,  3.0, 4.18,  0.0, 5.37, 4.57, 0.01, 0.06, 0.05, 0.01, 0.05, 0.03]
        ] 

df = pd.DataFrame(values, index=indexes, columns=columns)

corner = r'a \ b'
caption = 'Caption Caption Caption Caption Caption Caption'
columns_2 = np.array([corner] + list(df.columns))
caption_2 = np.array([caption] + [0]*(columns_2.size - 1))
values_2 = np.array(df.values)
indexes_2 = np.array(df.index)

table_tmp = np.hstack((indexes_2.reshape(indexes_2.size, 1), values_2))
table_tmp = np.vstack((columns_2.reshape(1, columns_2.size), table_tmp))
table_tmp = np.vstack((caption_2.reshape(1, caption_2.size), table_tmp))

tableMin = values_2.min()
tableMax = values_2.max()

###
t=Table(table_tmp.tolist())
t.setStyle(TableStyle(
            [('INNERGRID', (0,0), (-1,-1), 0.5, colors.black),
             ('BOX', (0,1), (-1,-1), 2, colors.black),
             ('LINEABOVE', (0,2), (-1,2), 2, colors.black),
             ('LINEBEFORE', (1,1), (1,-1), 2, colors.black),
             ('SPAN', (0,0), (-1,0)),
             ('ALIGN',(0,0),(0,0),'CENTER'),
             ('ALIGN',(0,1),(-1,-1),'CENTER')]))

for aux1_n, aux1_v in enumerate(values_2):
    for aux2_n, aux2_v in enumerate(aux1_v):
        if aux1_n >= 2 and aux2_n>=1:
            TH = max_thdh[[*max_thdh][aux2_n-1]]
            TF = values_2[aux1_n][aux2_n]
            if (tableMax == tableMin):
                c = 0
            elif (TH == tableMin or TH == tableMax):
                c = (TF - tableMin) / (tableMax - tableMin)
            elif TF <= TH:
                c = (TF - tableMin) / (TH - tableMin)
            else:
                c = (TF - TH) / (tableMax - TH)
                if TF <= TH:
                    G = (205.0+50.0*c)/255.0
                    R = (100.0+155.0*c)/255.0
                    B = (100.0+155.0*c)/255.0
                else:
                    R = (255.0-50.0*c)/255.0
                    G = (250.0-150.0*c)/255.0
                    B = (250.0-150.0*c)/255.0

t.setStyle(TableStyle([('BACKGROUND',(aux2_n,aux1_n),(aux2_n,aux1_n),(R,G,B))]))
elements.append(t)
doc.build(elements)