python-3.x - Python Reportlab 自动换行表
问题描述
我在 python 中有一个生成的列表,为了有一个很好的布局,我需要一个自动换行。由于在列表中列出列表,我无法使用 Paragraphe () (或者也许有人知道如何 - 我无法编写功能代码)
我在这个页面上找到了一个不会自动换行的代码,即使它这么说。
所以这里的问题是: 我如何在 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()`
解决方案
我修改并稍微重新安排了您的示例,以便它再次执行正确的操作,实际上引用的页面正在做它承诺的事情,但是缺少您必须传递 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()
推荐阅读
- d3.js - svg 如何在 svg 符号上添加边框?
- ms-access - 在导航表单中的子表单之间切换
- vega-lite - 如何在具有相同比例的 2 个系列的 vega-lite 中添加辅助 Y 轴?
- android - apk (.apk) 和应用程序包 (.aab) 之间的区别
- webgl - 如何为 WebGL 中的输出纹理或帧缓冲区提供偏移量?
- php - laravel 如何创建通知表?
- javascript - 如何从内部分配 PROMISE 之外的值
- typescript - 可以在 Typescript 库中的 package.json 中将 @types 库作为正常依赖项吗?
- disassembly - xc16获取反汇编(listing.disasm)文件的命令是什么?
- geneticsharp - 如何自定义染色体基因的值?