如何使用 Python 填写 PDF 表单 [英] How to fill PDF forms using Python

查看:52
本文介绍了如何使用 Python 填写 PDF 表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Adobe LiveCycle Designer ES 10.4 创建的 PDF 表单.我需要使用 Python 来填充它,以便我们可以减少手工劳动.我在网上搜索并阅读了一些文章,其中大部分文章都集中在 pdfrw 库上,我尝试使用它并从 PDF 表单 中提取了一些信息,如下所示

I have a PDF form created using Adobe LiveCycle Designer ES 10.4. I need to fill it using Python so that we can reduce manual labor. I searched the web and read some article most of them were focused around pdfrw library, I tried using it and extracted some information from PDF form as shown below

代码

from pdfrw import PdfReader
pdf = PdfReader('sample.pdf')
print(pdf.keys())
print(pdf.Info)
print(pdf.Root.keys())
print('PDF has {} pages'.format(len(pdf.pages)))

输出

['/Root', '/Info', '/ID', '/Size']
{'/CreationDate': "(D:20180822164509+05'30')", '/Creator': '(Adobe LiveCycle Designer ES 10.4)', '/ModDate': "(D:20180822165611+05'30')", '/Producer': '(Adobe XML Form Module Library)'}
['/AcroForm', '/MarkInfo', '/Metadata', '/Names', '/NeedsRendering', '/Pages', '/Perms', '/StructTreeRoot', '/Type']
PDF has 1 pages

我不确定如何进一步使用 pdfrw 从 PDF 表单访问可填写字段并使用 Python 填写它们是否可能.任何建议都会有所帮助.

I am not sure how further I can use pdfrw to access the fillable fields from the PDF form and fill them using Python is it possible. Any suggestions would be helpful.

推荐答案

您可以在此处找到表单字段:

You can find the form fields here:

pdf.Root.AcroForm.Fields

或这里

pdf.Root.Pages.Kids[page_index].Annots

这是一个 PdfArray 对象.基本上是一个列表.字段名称可在此处找到:

This is a PdfArray object. Basically a List. The Name of the field is found here:

pdf.Root.AcroForm.Fields[field_index].T

其他键包括值 .V.AP.N.Resources下有一堆显示信息,比如字体等

Other keys include the value .V There's a bunch of display information, like the font etc under .AP.N.Resources

但是,如果您更新字段的值并输出 pdf 文件.它可能仅在该字段具有焦点(即被点击)时显示该值.

However, if you update the value for a field and output the pdf file. It might only display the value when the field has focus i.e is clicked on.

我还没有想出如何解决这个问题.

I haven't figured out how to fix that yet.

这篇关于如何使用 Python 填写 PDF 表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆