我在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()`
答案 0 :(得分:1)
我进行了修改,并认真地重新整理了您的示例,使其再次执行正确的操作, 实际上,被引用的页面正在执行其承诺的操作,但是缺少您必须传递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()