如何在Python中高效批量生成带有不同变量输入的PDF。

huangapple go评论58阅读模式
英文:

How to mass produce PDF's efficiently in Python with different variable inputs into each PDF

问题

我最近开始了自动化投资者关系客户PDF生成的任务。我们需要批量发送PDF,但每个PDF的底角需要有独特的徽标和公司名称(我将徽标存储在一个文件夹中,相应的名称存储在一个txt文件中)。

此外,PDF的每一页是预定义的,但有一些变量是自定义的,比如“今年,收入增加了X%”。我也有每个公司的X值等。

期望输入:
公司名称和徽标

期望输出:
具有标准模板的PDF,但名称和徽标已更改

我尝试了以下内容:

来自FPDF的导入FPDF

pdfs = []

dct = {
    company1”:5
}

我尝试过的最简单的示例但不起作用
对于公司在打开(“company_names.txt”,“r”).readlines()
    pdf = FPDF方向=P’,单位=mm’,格式=A4’)
    pdf.add_page()
    pdf.set_font(‘helvetica’,‘bold’,10
    pdf.add_text公司
    pdf.add_textf收入增加了{dct[公司]}%”)
    pdf.add_pictureflogos/{company}.png”)#< - 这是除其他外不起作用

    pdfs.appendpdf
英文:

I have recently begun a task of automating the PDF generation for investor relations clients. We need to send out PDF's en masse, but each PDF needs to have a unique logo and company name at the bottom corner (I have the logos stored in a folder and corresponding names stored in a txt file).

Furthermore, each page of the PDF is predefined, but there are a few variables that are custom, such as "This year, revenue has increased by X%". I also have the X for each company, etc.

Desired input:
Company name and logo

Desired output:
PDF with standard template however with changed names and logo

I have tried the following:

from FPDF import FPDF

pdfs = []

dct = {
    &quot;company1&quot;: 5,
}

# minimal example of what I have tried, but doesn&#39;t work
for company in open(&quot;company_names.txt&quot;, &quot;r&quot;).readlines()
    pdf = FPDF(orientation = &#39;P&#39;, unit = &#39;mm&#39;, format = &#39;A4&#39;)
    pdf.add_page()
    pdf.set_font(&#39;helvetica&#39;, &#39;bold&#39;, 10)
    pdf.add_text(company)
    pdf.add_text(f&quot;Revenue has increased by {dct[company]}%&quot; )
    pdf.add_picture(f&quot;logos/{company}.png&quot;) # &lt;-- this, among other things, don&#39;t work

    pdfs.append(pdf)

Any help would be appreciated. Speed increases would also be appreciated, as it needs to generate thousands of PDF's.

答案1

得分: 1

我推荐使用reportlab.pdfgen模块。可以在这里找到文档:https://docs.reportlab.com/reportlab/userguide/ch2_graphics/

要插入可变文本,我建议将模板保存为.png文件。图像将成为生成的PDF的背景,文本显示在前面,营造文本一直存在的错觉。

通常PDF的生成速度无法更改,因为你选择了模块。但是,我发现很少需要亚秒级生成速度来实现此目的。

我做过类似的项目,效果如下:

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
import os

company_name = input("输入公司名称:") # 你可以在这里实现循环

pdf_filename = f"{company_name}_file.pdf"

height_page = 720 # 将这个值改为你的模板大小
width_page = 1270

c = canvas.Canvas(pdf_filename, pagesize=(width_page,height_page))

def add_text(text, x, y, size=12): # 创建这个函数可以更轻松地插入文本
    c.setFillColorRGB(255, 255, 255)
    c.setFont("Helvetica", size)
    c.drawString(x, y, text)

add_text(f"{str(company_name)}很棒!", 100, 200, 67) # 使用示例

# 要插入徽标,你可以使用c.drawImage()
c.drawImage(image, 0, 0, width=width_page,height=height_page,mask=None)

c.showPage() # 调用此方法创建新页面

c.save() # 保存文件
英文:

I would reccomend using the reportlab.pdfgen module. Docs can be found here: https://docs.reportlab.com/reportlab/userguide/ch2_graphics/

To insert variable text I would reccomend to have your template saved as a .png file.
The image will be the background on your generated PDF with the text in front, creating
the illusion that the text was there all along.

Generally the PDF's generation speed can't be changed, since you are opting for modules.
However, I find that it's rare to need sub-second generation speeds for this purpose.

I have made a project similar and it looks like this:

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
import os

company_name = input(&quot;Enter the company name: &quot;) # You can implement your loop here

pdf_filename = f&quot;{company_name}_file.pdf&quot;

height_page = 720 # Change this to your template size
width_page = 1270

c = canvas.Canvas(pdf_filename, pagesize=(width_page,height_page))

def add_text(text, x, y, size=12): # Creating this function makes it easier to insert text
    c.setFillColorRGB(255, 255, 255)
    c.setFont(&quot;Helvetica&quot;, size)
    c.drawString(x, y, text)

add_text(f&quot;{str(company_name)} is awesome!&quot;, 100, 200, 67) # Example of use

# To insert logos you would use the c.drawImage()
c.drawImage(image, 0, 0, width=width_page,height=height_page,mask=None)

c.showPage() # Call this to create a new page

c.save() # In order to save

huangapple
  • 本文由 发表于 2023年4月19日 16:27:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定