简体   繁体   中英

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?

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.

If you would provide a graphical example, would perhaps be simpler to understand the goal. Despite this, I will try to understand by interpret your question such as: -how do I automatically style row elements without knowing beforehand how many rows it will contain.

A: the section

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

Can actually become far more complicated. The short-cut versions as above (using -1) will be applied on all rows (or columns), however, the numbers within actually can be replaced with row, or column numbers, its a coordination system. Then you just need to create a variable that first measure your input data and put this variable into the style section.

see page 87 in the user guide as a start https://www.reportlab.com/docs/reportlab-userguide.pdf

Now there is a method for this: TableStyle.add(CommandSequence) . I wanted to add backcolor only for even rows.

I found the information in the ReportLab PDF Library User Guide page 85.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM