我使用一个表,但是,我在画布中绘制来控制可流动的位置,这是因为我在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()
自动回复:
诀窍在“height-h”中,h是表的高度,这取决于表的内容
我知道Postscript的引用在左下角。我猜PDF是一样的,所以你从y值中减去就可以了。打印函数中的开始和结束“y”值,查看它们是如何变化的,并根据句子的长度调整“y”值。函数如何知道“高度”是什么?我使用ReportLab,但如果您想发布一个特定的示例,可能会有所帮助。
描述文本在以样式[“Normal”]包装时上升。您可以尝试以样式[“BodyText”]包装文本。这将允许您的文本根据指定单元格的宽度对齐。您还可以包含类似于HTML文本格式的格式。
然后使用TableStyle格式化表中的内容,例如,彩色文本、中间段落、跨行/列等。
我将上面的代码编辑为工作版本(示例):
相关问题 更多 >
编程相关推荐