从Python中的现有PDF创建新的PDF

2024-10-02 18:23:31 发布

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

我正在努力如何用Python中的另一个PDF作为模板来创建PDF报告。 我有一个PDF文件(模板.pdf)可以作为每天创建报表的模板。 模板.pdf如下所示:

 ABC Corp

^{pr2}$

SalesName订单数量确认数量发货数量




我需要按程序填写ReportDate和销售数据,并准备PDF格式的报告,如下所示: ABC公司


Daily Sales Report         Report Date: 20120117                        

SalesName订单数量确认数量发货数量


杰森1000 900 50


彼得500 50 450


穆拉利2000 1000 900


可以假设销售人员的数量是固定的(即报表中的行数是固定的)。在


Tags: 文件订单report模板数量pdf报表报告
1条回答
网友
1楼 · 发布于 2024-10-02 18:23:31

您可以尝试reportlab,因为您的pdf模板相对容易。我不确定您将从何处提取数据,以及您的模板到底是什么样子,但我编写了一个小代码,让您查看或启动。最好能看看reportlab的用户指南。在

您可以查看html到pdf的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)

相关问题 更多 >