我用Reportlab复制此表单的方法正确吗?

2024-06-02 06:05:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要使用Python/reportlab生成这里看到的表单。在

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

我正试图这样做,通过创建一个自定义流动的标题在顶部(与框画)然后有一个表流动为珠宝表下面。本例中显示的珠宝表可能是多个表。我很难让我的画头到“流”。它被绘制出来,但是我的表数据覆盖了它,而不是显示在它下面。在

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


Tags: comhttp标题表单www绘制flickrsizes
3条回答

我同意dugres的观点,你不需要为flickr中显示的特定表单提供任何可定制的流。您可以使用表和表样式来完成您的工作。在

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

对于花哨的输出,不错的图形效果。你需要按照达格雷斯的建议去做。在

关于开发表的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()

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

我不认为这里需要定制的流程。在

您可以使用表(和表样式)来完成“header”。在

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

相关问题 更多 >