在表reportlab中换行文本?

时间:2011-01-18 15:46:24

标签: python reportlab

我使用了一个表但是,我在画布中绘制来控制flowable的位置,这是因为我在pdf中有一个模板,我与pyPDF合并。

换行是在一张桌子上完成的,但是文字上升了,而不是我希望的那样。

c是画布

代码

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import Paragraph, Table
from reportlab.lib.units cm

width, height = A4
styles = getSampleStyleSheet()

def coord(x, y, unit=1):
    x, y = x * unit, height -  y * unit
    return x, y

descrpcion = Paragraph('long paragraph', styles["Normal"])
partida = Paragraph('1', styles["Center"])
candidad = Paragraph('120', styles["Center"])
precio_unitario = Paragraph('$52.00', styles["right"])
precio_total = Paragraph('$6240.00', styles["right"])

data= [[partida, candidad, descrpcion, precio_unitario, precio_total]]
table = Table(data, colWidths=[2.05 * cm, 2.7 * cm, 9.6 * cm,
                               2.65 * cm, 2.7 * cm])

c = canvas.Canvas(PDF, pagesize=A4)
table.wrapOn(c, width, height)
table.drawOn(c, *coord(1.8, 9.6, cm))
c.save()

http://img600.imageshack.us/img600/3203/reportld.jpg

3 个答案:

答案 0 :(得分:19)

当你将它包装成样式[“Normal”]时,描述文本会上升你可以尝试将文本换成样式[“BodyText”]这将允许你的文本根据单元格的宽度自行对齐你指定。您还可以包含与HTML文本格式类似的格式。

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

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

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import cm
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import Paragraph, Table, TableStyle
from reportlab.lib.enums import TA_JUSTIFY, TA_LEFT, TA_CENTER
from reportlab.lib import colors

width, height = A4
styles = getSampleStyleSheet()
styleN = styles["BodyText"]
styleN.alignment = TA_LEFT
styleBH = styles["Normal"]
styleBH.alignment = TA_CENTER

def coord(x, y, unit=1):
    x, y = x * unit, height -  y * unit
    return x, y

# Headers
hdescrpcion = 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)

# Texts
descrpcion = 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()

答案 1 :(得分:4)

<强>自动答复:

def coord(x, y, height, unit=1):
    x, y = x * unit, height -  y * unit
    return x, y

w, h = table.wrap(width, height)
table.wrapOn(c, width, height)
table.drawOn(c, *coord(ml - 0.05, y + 4.6, height - h, cm))

诀窍在于“高度 - h”,h是表格的高度,这取决于表格的内容

答案 2 :(得分:0)

我知道Postscript的引用是左下角。我猜测PDF是相同的,所以你从y值中减去下去。在函数中打印开始和结束“y”值以查看它们如何变化,并根据句子的长度调整“y”值。功能如何知道“高度”是什么?我使用ReportLab,但如果您想发布一个,可能会对某个具体示例有所帮助。