使用ReportLab将数据框转换为PDF中的GRID。我如何在网格的一列中进行折线

时间:2019-06-03 21:01:57

标签: python reportlab

我需要在网格中插入一个分界线,因为一列的值太大而无法放入PDF中:

enter image description here

我的代码:

for i, (_, rowCampos) in enumerate(dfBaseCampos.iterrows()):
    dados.append([str(rowCampos['CAMPO']),
    str(rowCampos['DESC_CAMPO']),
    str(rowCampos['TIPO_CAMPO'])])

tabela = Table(dados, style=([
    ('GRID', (0, 0), (-1, -1), 0.25, colors.black),
    ('BOX', (0, 0), (-1, -1), 0.25, colors.black, None, (2, 2, 1)),
    ('BACKGROUND', (2, 0), (2, 0), colors.lightcyan),
    ('BACKGROUND', (0, 0), (0, 0), colors.lightcyan),
    ('BACKGROUND', (1, 0), (1, 0), colors.lightcyan),
    ('FONTSIZE', (0, 0), (-1, -1), 5),
    ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black)
]))

1 个答案:

答案 0 :(得分:0)

您需要使用Paragraph之类的Flowable对象。

l1 = Paragraph("Hello python", styles['Normal'])

此解决方案对我有用:

from reportlab.platypus import Paragraph
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import Table, TableStyle
from reportlab.lib import colors

styles = getSampleStyleSheet()
for i, (_, rowCampos) in enumerate(dfBaseCampos.iterrows()):
    l1 = Paragraph(str(rowCampos['CAMPO']), styles['Normal'])
    l2 = Paragraph(str(rowCampos['DESC_CAMPO']), styles['Normal'])
    l3 = Paragraph(str(rowCampos['TIPO_CAMPO']), styles['Normal'])
    dados.append([l1, l2, l3])

tabela =Table(dados)
tabela.setStyle(TableStyle([
    ('GRID', (0, 0), (-1, -1), 0.25, colors.black),
    ('BOX', (0, 0), (-1, -1), 0.25, colors.black, None, (2, 2, 1)),
    ('BACKGROUND', (2, 0), (2, 0), colors.lightcyan),
    ('BACKGROUND', (0, 0), (0, 0), colors.lightcyan),
    ('BACKGROUND', (1, 0), (1, 0), colors.lightcyan),
    ('FONTSIZE', (0, 0), (-1, -1), 5),
    ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black)
]))