Ati*_*esh 2 python pdf adobe pdf-form pdfrw
我有一个PDF form使用Adobe LiveCycle Designer ES 10.4. 我需要使用它来填充它,Python以便我们可以减少体力劳动。我在网上搜索并阅读了一些文章,其中大部分文章都集中在pdfrw图书馆,我尝试使用它并从中提取了一些信息PDF form如下所示
代码
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)))
Run Code Online (Sandbox Code Playgroud)
输出
['/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
Run Code Online (Sandbox Code Playgroud)
我不确定如何进一步pdfrw访问 PDF 表单中的可填写字段并使用它们填写Python是否可能。任何的意见都将会有帮助。
我编写了一个基于“pdfrw”、“pdf2image”、“Pillow”、“PyPDF2”的库,称为 fillpdf (pip install fillpdf和 poppler 依赖项conda install -c conda-forge poppler)
基本用法:
from fillpdf import fillpdfs
fillpdfs.get_form_fields("blank.pdf")
# returns a dictionary of fields
# Set the returned dictionary values a save to a variable
# For radio boxes ('Off' = not filled, 'Yes' = filled)
data_dict = {
'Text2': 'Name',
'Text4': 'LastName',
'box': 'Yes',
}
fillpdfs.write_fillable_pdf('blank.pdf', 'new.pdf', data_dict)
# If you want it flattened:
fillpdfs.flatten_pdf('new.pdf', 'newflat.pdf')
Run Code Online (Sandbox Code Playgroud)
更多信息在这里: https: //github.com/t-houssian/fillpdf
如果某些字段未填写,您可以使用 fitz ( pip install PyMuPDF) 和 PyPDF2 ( pip install PyPDF2) ,如下所示根据需要更改点:
import fitz
from PyPDF2 import PdfFileReader
file_handle = fitz.open('blank.pdf')
pdf = PdfFileReader(open('blank.pdf','rb'))
box = pdf.getPage(0).mediaBox
w = box.getWidth()
h = box.getHeight()
# For images
image_rectangle = fitz.Rect((w/2)-200,h-255,(w/2)-100,h-118)
pages = pdf.getNumPages() - 1
last_page = file_handle[pages]
last_page._wrapContents()
last_page.insertImage(image_rectangle, filename=f'image.png')
# For text
last_page.insertText(fitz.Point((w/2)-247 , h-478), 'John Smith', fontsize=14, fontname="times-bold")
file_handle.save(f'newpdf.pdf')
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以在此处找到表单字段:
pdf.Root.AcroForm.Fields
Run Code Online (Sandbox Code Playgroud)
或在这里
pdf.Root.Pages.Kids[page_index].Annots
Run Code Online (Sandbox Code Playgroud)
这是一个 PdfArray 对象。基本上是一个列表。字段名称可在此处找到:
pdf.Root.AcroForm.Fields[field_index].T
Run Code Online (Sandbox Code Playgroud)
其他键包括值 .V 有一堆显示信息,例如 .AP.N.Resources 下的字体等
但是,如果您更新字段的值并输出 pdf 文件。它可能仅在字段具有焦点时显示值,即单击。
我还没有想出如何解决这个问题。