【问题标题】:Removing rounded border from table line in ReportLab从 ReportLab 中的表格行中删除圆角边框
【发布时间】:2022-04-04 04:46:55
【问题描述】:

我知道 ReportLab 可以使用“linejoin”和“linecap”设置使线条四舍五入。

对于表格,LINEBEFORE 或 LINEAFTER 命令将在表格上放置一条分隔两列的水平线。

有没有办法让这条线没有圆边?它默认为圆角。

此代码将创建一个示例表。如何使那条红色垂直线成为没有圆边的矩形?或者解决方案只是在列之间添加一个细列并用红色填充它。

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)

【问题讨论】:

    标签: reportlab


    【解决方案1】:

    这不是一个完美的答案,但是在左侧制作一些薄单元并填充它们可以解决问题

    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)
    

    当然,您也必须调整列宽

    【讨论】:

      【解决方案2】:

      你可以改变默认的linecap,通过将你想要的linecap添加到Tablestyle参数,它是第六个参数。这样就不需要填充列/行。

      t=Table(data,style=[('LINEBEFORE',(2,1),(2,-2),6,colors.pink,'squared')]
      

      linecap 的选项定义为

      LINECAPS={None: None, 'butt':0,'round':1,'projecting':2,'squared':2}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-05
        • 2015-08-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-29
        • 2014-04-08
        • 2014-09-28
        相关资源
        最近更新 更多