Python Reportlab的文本自动换行表格

3
我有一个在Python中生成的列表,为了有一个好的布局,我需要对它进行自动换行。由于它是一个列表中的列表,我无法使用Paragraphe()(或许有人知道怎么用-但我还没有写出可运行的代码)。
我在page上找到了一段代码,但它不会进行自动换行,尽管它声称可以这样做。
所以问题来了: 如何对我的单元格中的文本进行自动换行,使表适合页面并能看到所有文本?
以下是我的代码(简化版):
from reportlab.lib.pagesizes import A4
from reportlab.lib.pagesizes import letter, cm
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import LongTable, TableStyle, BaseDocTemplate, Frame, PageTemplate
from reportlab.lib import colors
from reportlab.platypus import Paragraph, Table, TableStyle
########################################################################

def test():
    doc = BaseDocTemplate(
        "question.pdf",
        pagesize=A4,
        rightMargin=72,
        leftMargin=72,
        topMargin=50,
        bottomMargin=80,
        showBoundary=False)

elements = []
data = [['A', 'B', 'C', 'dddddddddddd', 'D'],
        ['00', '0dddddddddddddddddddddddddddddddddddd1', '02', 'fff', '04'],
        ['10', '11', '12', 'dfg', '14'],
        ['20', '21', '22', 'ddddddddddddddddddddddddddddddddddddddddddddddddddddddd23', '24'],
        ]

t = LongTable(data)

tableStyle = [
    ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
    ('BOX', (0, 0), (-1, -1), 0.25, colors.black),
]
t.setStyle(TableStyle(tableStyle))
elements.append(t)

styles = getSampleStyleSheet()
styleN = styles['Normal']


frame = Frame(doc.leftMargin, doc.bottomMargin, doc.width, doc.height - 2 * cm, id='normal')
template = PageTemplate(id='longtable', frames=frame)
doc.addPageTemplates([template])


doc.build(elements)


if __name__ == '__main__':
    test()`
1个回答

5

我修改了你的示例并稍微重新排列了一下,以便它再次执行正确的操作。实际上,引用页面正在执行其承诺的操作,但缺少传递colWidth参数的说明。希望你从这里继续:

from reportlab.lib.pagesizes import A4
from reportlab.lib.pagesizes import letter, cm
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import LongTable, TableStyle, BaseDocTemplate,Frame, PageTemplate
from reportlab.lib import colors
from reportlab.platypus import Paragraph, Table, TableStyle

def reprFrame(frame):
    _dict = vars(frame)
    for key in sorted(list(_dict.keys())):
        print(key, ": ", _dict[key])

def test():
    doc = BaseDocTemplate(
        "question.pdf",
        pagesize=A4,
        rightMargin=72,
        leftMargin=72,
        topMargin=50,
        bottomMargin=80,
        showBoundary=False)

    elements = []
    data = [['A', 'B', 'C', 'dddddddddddd', 'D'],
        ['00', '0dddddddddddddddddddddddddddddddddddd1', '02', 'fff', '04'],
        ['10', '11', '12', 'dfg', '14'],
        ['20', '21', '22', 'ddddddddddddddddddddddddddddddddddddddddddddddddddddddd23', '24'],
        ]



    tableStyle = [
        ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
        ('BOX', (0, 0), (-1, -1), 0.25, colors.black),
        ]


    styles = getSampleStyleSheet()
    styleN = styles['Normal']

    styleN.wordWrap = 'CJK'

    data2 = [[Paragraph(cell, styleN) for cell in row] for row in data]

    frame = Frame(doc.leftMargin, doc.bottomMargin, doc.width, doc.height - 2 * cm, id='normal')

    reprFrame(frame)

    colwidths = [frame._width/5. for i in range(5)]

    t = LongTable(data2, colWidths=colwidths)
    t.setStyle(TableStyle(tableStyle))
    elements.append(t)

    template = PageTemplate(id='longtable', frames=frame)
    doc.addPageTemplates([template])
    doc.build(elements)


if __name__ == '__main__':
    test()

谢谢您提供这篇非常有帮助的文章。现在我遇到了一个问题,就是有时候一个单元格或一行数据会超过一页,导致程序崩溃。您知道如何解决这个问题吗? - Mady
你可以从以下主题中获取帮助:https://dev59.com/f-o6XIcBkEYKwwoYTzEw - skidzo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接