如何在报告实验室中换行表格文本?

26 投票
4 回答
50178 浏览
提问于 2025-04-16 10:11

我使用了一个表格,但我是在一个画布上绘制内容,以便控制可流动对象的位置。这是因为我有一个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()

4 个回答

0

我知道Postscript的参考点是在左下角。我猜PDF也是这样,所以要向下移动就得从y值中减去一些。可以在函数里打印出开始和结束的“y”值,看看它们是怎么变化的,然后根据句子的长度来调整“y”值。还有,这个函数是怎么知道“高度”是多少的呢?我用的是ReportLab,但如果你愿意发个具体的例子,我可能能帮上忙。

5

自动回复:

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 是表格的高度,而这个高度是根据表格里的内容来决定的。

31

把描述文本放在 styles["Normal"] 里面后,它的显示效果会变好。你可以试着把文本放在 styles["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()

撰写回答