'Removing rounded border from table line in ReportLab
I know ReportLab has options for making lines rounded with their 'linejoin' and 'linecap' settings.
For a table, the commands LINEBEFORE or LINEAFTER will put a horizontal line separating two columns on your table.
Is there a way to make this line with no rounded edges? It defaults to rounded edges.
This code will make an example table. How can I make that red vertical line a rectangle with no rounded edges? Or is the solution just adding a thin column between the columns and filling it with red.
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter, inch
from reportlab.platypus import Image, Paragraph, SimpleDocTemplate, Table
from reportlab.lib.styles import getSampleStyleSheet
doc = SimpleDocTemplate("delete_me.pdf", pagesize=letter)
# container for the 'Flowable' objects
elements = []
styleSheet = getSampleStyleSheet()
P0 = Paragraph('''
<b>A pa<font color=red>r</font>a<i>graph</i></b>
<super><font color=yellow>1</font></super>''',
styleSheet["BodyText"])
P = Paragraph('''
<para align=center spaceb=3>The <b>ReportLab Left
<font color=red>Logo</font></b>
Image</para>''',
styleSheet["BodyText"])
data= [['A', 'B', 'C', P, 'D'],
['00', '01', '02', P, '04'],
['10', '11', '12', P, '14'],
['20', '21', '22', '23', '24'],
['30', '31', '32', '33', '34']]
t=Table(data,style=[('LINEBEFORE',(2,1),(2,-2),6,colors.pink)]
)
t._argW[3]=1.5*inch
elements.append(t)
# write the document to disk
doc.build(elements)
Solution 1:[1]
This isn't a perfect answer, but making some thin cells to the left and filling them can do the trick
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter, inch
from reportlab.platypus import Image, Paragraph, SimpleDocTemplate, Table
from reportlab.lib.styles import getSampleStyleSheet
doc = SimpleDocTemplate("delete_me.pdf", pagesize=letter)
# container for the 'Flowable' objects
elements = []
styleSheet = getSampleStyleSheet()
P0 = Paragraph('''
<b>A pa<font color=red>r</font>a<i>graph</i></b>
<super><font color=yellow>1</font></super>''',
styleSheet["BodyText"])
P = Paragraph('''
<para align=center spaceb=3>The <b>ReportLab Left
<font color=red>Logo</font></b>
Image</para>''',
styleSheet["BodyText"])
data= [['A', 'B', 'C', P, 'D'],
['00', '01', '02', P, '04'],
['10', '11', '12', P, '14'],
['20', '21', '22', '23', '24'],
['30', '31', '32', '33', '34']]
for i in range(len(data)): # Create slim invisible column
data[i] = [' '] + data[i]
t=Table(data,style=[
('BACKGROUND', (0,1), (0,-2), colors.pink),
('LINEBEFORE',(2,1),(2,-2),6,colors.pink)
])
t._argW[3]=1.5*inch
elements.append(t)
# write the document to disk
doc.build(elements)
Of course, you'll have to adjust your column widths too
Solution 2:[2]
You can change the default linecap, by adding your desired linecap to the style argument of the Table, it is the sixth argument. This prevents the need for filler columns/rows.
t=Table(data,style=[('LINEBEFORE',(2,1),(2,-2),6,colors.pink,'squared')]
The options for linecap are defined as
LINECAPS={None: None, 'butt':0,'round':1,'projecting':2,'squared':2}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | benjo |
| Solution 2 | Chris Puglia |
