【发布时间】:2018-12-17 20:31:52
【问题描述】:
我在 python 中有一个生成的列表,为了有一个漂亮的布局,我需要一个自动换行。由于在列表中添加列表,我无法使用 Paragraphe ()(或者可能有人知道如何使用 - 我无法编写功能代码)
我在这个page 上找到了一个代码,它不会自动换行,即使它这么说。
那么问题来了: 如何在 mycells 中对文本进行自动换行,以便表格适合页面并且可以看到所有文本?
这是我的代码(简而言之):
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()`
【问题讨论】:
标签: python-3.x word-wrap reportlab