栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将文字换成表格reportlab?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将文字换成表格reportlab?

当您将描述文字换成styles [“ Normal”]时,描述文本会上升。您可以尝试将文本换成styles [“
BodyText”],这样您的文字可以根据您指定的单元格的宽度进行对齐。您还可以包括类似于HTML文本格式的格式。

然后使用TableStyle格式化表格中的内容,例如,彩色文本,中间段落,跨行/列等。

我将上面的代码编辑为工作版本(示例):

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()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/659518.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号