推荐答案当你把它包裹在一个 style["Normal"] 你可以尝试将你的文本包裹在一个 style["BodyText"] 这个将允许您的文本根据您指定的单元格的宽度自行对齐.您还可以包含类似于 HTML 文本格式的格式.The description text went up as you wrap it in a styles["Normal"] You can try to wrap your text in a styles["BodyText"] This will allow your text to align themselves according to the width of the cell you specify. You could also include formatting which is similar to HTML text formatting.然后使用 TableStyle 对表格中的内容进行格式化,例如,彩色文本、居中段落、跨行/列等.Then use TableStyle to format the content in the table, for example, color text, center paragraph, span rows/columns and so on.我将上面的代码编辑为工作版本(示例):I edited the code above to a working version (example):from reportlab.pdfgen import canvasfrom reportlab.lib.pagesizes import A4from reportlab.lib.units import cmfrom reportlab.lib.styles import getSampleStyleSheetfrom reportlab.platypus import Paragraph, Table, TableStylefrom reportlab.lib.enums import TA_JUSTIFY, TA_LEFT, TA_CENTERfrom reportlab.lib import colorswidth, height = A4styles = getSampleStyleSheet()styleN = styles["BodyText"]styleN.alignment = TA_LEFTstyleBH = styles["Normal"]styleBH.alignment = TA_CENTERdef coord(x, y, unit=1): x, y = x * unit, height - y * unit return x, y# Headershdescrpcion = Paragraph('''<b>descrpcion</b>''', styleBH)hpartida = Paragraph('''<b>partida</b>''', styleBH)hcandidad = Paragraph('''<b>candidad</b>''', styleBH)hprecio_unitario = Paragraph('''<b>precio_unitario</b>''', styleBH)hprecio_total = Paragraph('''<b>precio_total</b>''', styleBH)# Textsdescrpcion = Paragraph('long paragraph', styleN)partida = Paragraph('1', styleN)candidad = Paragraph('120', styleN)precio_unitario = Paragraph('$52.00', styleN)precio_total = Paragraph('$6240.00', styleN)data= [[hdescrpcion, hcandidad,hcandidad, hprecio_unitario, hprecio_total], [partida, candidad, descrpcion, precio_unitario, precio_total]]table = Table(data, colWidths=[2.05 * cm, 2.7 * cm, 5 * cm, 3* cm, 3 * cm])table.setStyle(TableStyle([ ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), ('BOX', (0,0), (-1,-1), 0.25, colors.black), ]))c = canvas.Canvas("a.pdf", pagesize=A4)table.wrapOn(c, width, height)table.drawOn(c, *coord(1.8, 9.6, cm))c.save() 这篇关于在表格报告实验室中换行文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 23:16