1

我需要使用 Python/reportlab 生成此处看到的表单。

http://www.flickr.com/photos/49740282@N06/4563137758/sizes/o/

我试图通过为顶部的标题创建一个自定义的可流动对象(绘制框),然后为下面的珠宝表创建一个可流动的表格。此示例中显示为 JEWELRY 表的内容可能是多个表。我无法让我绘制的标题“流动”。它被绘制了,但是我的表格数据覆盖了它而不是出现在它下面。

这是我在reportlab 的第一个项目。在我真正开始调试之前,我想从有报告实验室经验的人那里知道我的方法在这里是否正确。谢谢!

4

3 回答 3

0

我认为这里不需要自定义 flowable。

您可以只使用表格(和表格样式)来做“标题”。

如果您需要一些花哨的背景,另一个简单的解决方案是绘制图像(如 JPG),然后在其上绘制变量字符串。

于 2010-06-29T15:47:46.697 回答
0

我同意 dugres 的观点,即对于 flickr 中显示的特定表单,您不需要任何可自定义的 flowable。您可以只使用 Table 和 TableStyle 来完成您的工作。

在您开始深入了解 reportlab 之前,需要考虑的是,您的表格不会太长,以至于无法进入下一页。然后表格样式将需要手动编辑。下一页表格上的 SPAN 单元格将返回错误。但对于单页解决方案,reportpdf 是一个不错的选择。

对于花哨的输出,漂亮的图形效果。您需要按照 dugres 的建议进行操作。

有关开发表的 kickstart 代码:

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4, 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', styleBH)
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()
于 2012-04-23T07:00:26.190 回答
-1

我无法帮助您使用reportlab,因为我不是很有经验的用户(在一些让我发疯的问题之后,我留下了使用它的想法:))。但是,如果您考虑使用其他工具在 python 中生成您的 pdf,我建议您查看xhtml2pdf - 如果您没有深入了解 reportlab,这可能是一个不错的选择。如果您熟悉 html,这可能更容易使用。这里的想法很简单:它将您提供的 html 转换为 pdf 文件。当然,您需要以某种方式生成 html 代码(我为此使用 django 模板)。

于 2010-06-25T09:02:44.893 回答