我使用了一个表格,但是我在画布中绘制来控制流动对象的位置,这是因为我在 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()