您可以嘗試reportlab作爲您的pdf模板相對容易。我不確定你將從哪裏獲取數據,以及模板的外觀如何,但我寫了一個小代碼讓你看到或啓動。如果你能通過reportlab的用戶指南,那會更好。
您可以查看html for pdf for python解決方案。它應該是你正在尋找的東西。
ReportLab的例子:
import datetime
from reportlab.pdfgen import canvas
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.pagesizes import A4, letter, inch, cm
from reportlab.platypus import Paragraph, SimpleDocTemplate, Table, TableStyle, Spacer, KeepTogether, CondPageBreak
from reportlab.lib import colors
from reportlab.lib.enums import TA_JUSTIFY, TA_LEFT, TA_CENTER
styles = getSampleStyleSheet()
class Sales(object):
def __init__(self):
self.salesname = 'Jason'
self.orderqty = 1000
self.confirmqty = 900
self.shipqty = 50
jason = Sales()
# get current date
date = datetime.date.today()
jahr = date.year
monat = date.month
tag = date.day
story = []
# Styles
styleN = styles["BodyText"]
styleN.alignment = TA_LEFT
styleBH = styles["Normal"]
styleBH.alignment = TA_CENTER
styleH = styles["Heading2"]
# Static texts
title = "ABC Corp"
subtitle = "Daily Sales Report"
# Headers
hdescrpcion = Paragraph('''<b>SalesName</b>''', styleBH)
hpartida = Paragraph('''<b>OrderQty</b>''', styleBH)
hcandidad = Paragraph('''<b>ConfirmedQty</b>''', styleBH)
hprecio_unitario = Paragraph('''<b>ShippedQty</b>''', styleBH)
# Date
mydate = Paragraph('''Report Date: '''+str(jahr)+'''-'''+str(monat)+'''-'''+str(tag), styleN)
# 2 col data
data_2col =[[subtitle,mydate]]
table2 = Table(data_2col, colWidths=[5.05 * cm, 5* cm])
# 4 col data
data= [[hdescrpcion, hpartida,hcandidad, hprecio_unitario],
[jason.salesname, jason.orderqty, jason.confirmqty, jason.shipqty]]
table = Table(data, colWidths=[2.05 * cm, 2.7 * cm, 5 * cm,
3* cm])
table.setStyle(TableStyle([
('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
('BOX', (0,0), (-1,-1), 0.25, colors.black),
]))
story.append(Paragraph(title,styleH))
story.append(Spacer(1, 12))
story.append(table2)
story.append(Spacer(1, 12))
story.append(table)
doc = SimpleDocTemplate('template.pdf',pagesize = A4,rightMargin=18,leftMargin=18,
topMargin=18,bottomMargin=18, showBoundary=False, allowSplitting = True)
doc.build(story)
已經有了一個看看這些網站:Python的PDF生成與Snakelets(http://www.hoboes.com/Mimsy/hacks/python-pdf-generation/)和[ReportLab - 開源PDF庫](http://www.reportlab.com/software/opensource/rl-toolkit/)? – gecco 2012-01-17 07:59:04
到目前爲止你有什麼代碼? – razlebe 2012-01-17 08:34:28