python中填充空值_在python中填充可编辑的pdf

python中填充空值

介绍 (Introduction)

Unlike traditional paper-based forms which are often a headache to complete by hand, PDF fillable forms are convenient and offer a broad range of user interactivity. It is fast, easy, has fewer errors, easy archiving, and accessing and completely reusable.

与传统的基于纸质的表单通常难以手动完成相比, PDF可填充表单非常方便,并提供了广泛的用户交互性。 它快速,简便,错误少,易于存档,访问且完全可重用。

Many companies these days have started using editable Pdf forms instead of paper-based forms for filling information for their clients. In most of the organizations (like banks, universities), users fill up a form on a website, and later that data is fetched from DB to fill the Pdf, with pre-defined template and fields, and store it for later use.

如今,许多公司已经开始使用可编辑的Pdf表单,而不是基于纸质的表单来为其客户填充信息。 在大多数组织(如银行,大学)中,用户在网站上填写表格,然后从数据库中获取数据以使用预定义的模板和字段填充Pdf,并将其存储以备后用。

In this article, I’m gonna use python to automate this process of filling data in Pdf. Let’s get started.

在本文中,我将使用python自动执行Pdf中填充数据的过程。 让我们开始吧。

上课 (Making our Class)

So first, I’m gonna make a class which will have all the methods for the Pdf. I am naming it as ProcessPdf. In the __init__() function in this class, I’ll set temporary folder path where all our temporary files (pdfs) will be stored, and an output file path where the final pdf will be stored.

所以首先,我要创建一个类,其中将包含Pdf的所有方法。 我将其命名为ProcessPdf。 在此类的__init __()函数中,我将设置用于存储所有临时文件(pdf)的临时文件夹路径,以及将用于存储最终pdf的输出文件路径。

class ProcessPdf:    def __init__(self, temp_directory, output_file):        self.temp_directory = temp_directory        self.output_file = output_file

Next, I’ll make a function for writing data in the Pdf. In this function, I’ll pass path to my Pdf template file and the data object containing all the data to be filled. Then I’ll read the Pdf using Pdfrw Library in python.

接下来,我将创建一个用于在Pdf中写入数据的函数。 在此函数中,我将传递路径到我的Pdf模板文件和包含所有要填充数据的数据对象。 然后,我将使用python中的Pdfrw库读取Pdf。

Note — The keys in the data object should be the same as those in Pdf.

注—数据对象中的键应与Pdf中的键相同。

def add_data_to_pdf(self, template_path, data):
template = pdfrw.PdfReader(template_path)

Loop over all the pages in Pdf, and for each page, loop over all the editable fields (called annots). For each page, all the annotations are stored in ‘/Annots’ key, and all the key names for each field/annot are stored in ‘/T’ key.

循环遍历Pdf中的所有页面,并为每个页面循环遍历所有可编辑字段(称为annots )。 对于每个页面,所有注释都存储在“ / Annots”键中,每个字段/注释的所有键名称都存储在“ / T”键中。

for page in template.pages:
annotations = page[‘/Annots’]
for annotation in annotations:
key = annotatio
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值