使用 Python 从现有 PDF 创建新 PDF

发布于 2024-12-27 12:26:49 字数 687 浏览 2 评论 0原文

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

 ABC Corp

Daily Sales Report         Report Date:                                 

SalesName OrderQty ConfirmedQty ShippedQty




我需要以编程方式填写 ReportDate 和销售数据并准备 PDF 格式的报告,如下所示: ABC Corp


Daily Sales Report         Report Date: 20120117                        

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


Jason 1000 900 50


Peter 500 50 450


Murali 2000 1000 900


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

I'm struggling how to create a PDF report using another PDF as template in Python.
I have a PDF file ( Template.pdf) that can be used as the template to create report every day.
Template.pdf looks like following:

 ABC Corp

Daily Sales Report         Report Date:                                 

SalesName OrderQty ConfirmedQty ShippedQty




I need to programatically fill ReportDate, and sales data and prepare the report in PDF format as shown below:
ABC Corp


Daily Sales Report         Report Date: 20120117                        

SalesName OrderQty ConfirmedQty ShippedQty


Jason 1000 900 50


Peter 500 50 450


Murali 2000 1000 900


It can be assumed that no. of sales person are fixed (i.e. no. of rows are fixed in report).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

思念满溢 2025-01-03 12:26:49

您可以尝试reportlab,因为您的pdf模板相对简单。我不确定您将从哪里获取数据以及您的模板到底是什么样子,但我编写了一个小代码来让您查看或开始使用。如果您能阅读一下reportlab 的用户指南就更好了。

您可以查看 html to pdf for python 解决方案。它也应该是您正在寻找的。

报告实验室示例:

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)

You could try reportlab as your pdf template is relatively easy. I'm not sure where you'll pull your data from and how exactly does your template looks like, but I'd written a small code to let you see or get started up. It's better if you could go through reportlab's user guide.

You could check out html to pdf for python solution. It should be what you're looking for as well.

Reportlab example:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文