Search code examples
pythonreportlab

Style reportlab table


conn = sqlite3.connect('abc.db')
cursor = conn.execute('''ac''')

column1Heading = "a"
column2Heading = "b"
column3Heading = "c"
column4Heading= "d"
column5Heading = "e"
column6Heading = "f"

data=[]
for row in cursor:
    D=str(row[0])
    t=str(row[1])
    C=str(row[2])
    ca = str(row[3]).split(',')
    cm = str(row[4]).split(',')
    data.append([column1Heading,D])
    data.append([column2Heading,t])
    data.append([column4Heading,len(c)])
    if len(ca) is not 0:
        data.append([column3Heading,column5Heading,column6Heading])
    for i in range(len(cm)):
        Ca=str(ca[i])
        Cl=str(cm[i])
        data.append([C,Ca,Cm])


style=[
 ('GRID',(0,0),(-1,-1),0.5,colors.gray),
 ('ALIGN',(0,1),(-1,-1),'CENTER'),
 ('SPAN',(0,0),(-1,0))
]


s = getSampleStyleSheet()
s = s["Normal"]
s.alignment=TA_CENTER
s.wordWrap = 'CJK'
t=Table(data)
t.setStyle(TableStyle(style))
Story.append(t)

So I want to repeat style if for example "name" is in first row and it also in 50th or 100th row and i want to give the same style for them also then what i have to do?


Solution

  • Most of the people did not understand my question because I think my question wasn't proper but I found answer for my question and I want to share it so that others can make use of it. So imagine you have hundreds of rows and columns and you want to color specific row based on its value.

    style=[]
    
    style.append(['GRID',(0,0),(-1,-1),colors.black])
    for row, values in enumerate(data):
       for colum, value in enumerate(values):
           if value == "Fruit":
               style.append('BACKGROUND',(column,row),(column,row),colors.red)
    

    This would color the specific cell you mentioned. And if you want to color the next row or cell of the value then just make addition operation and you can interchange row and column to play with them to check how it works.