【问题标题】:Batch fill PDF forms from python or bash从 python 或 bash 批量填写 PDF 表单
【发布时间】:2012-05-15 14:49:19
【问题描述】:

我有一个 PDF 表格,需要多次填写(准确地说是时间表)。现在,由于我不想手动执行此操作,因此我正在寻找一种方法来使用 python 脚本或可在 bash 脚本中使用的工具来填写它们。

有人有这方面的经验吗?

【问题讨论】:

标签: python forms pdf automation


【解决方案1】:

对于 Python,您需要 fdfgen lib 和 pdftk

@Hugh Bothwell 的评论是 100% 正确的,所以我将通过一个有效的实现来扩展这个答案。

如果您使用的是 Windows,您还需要确保 python 和 pdftk 都包含在系统路径中(除非您想使用长文件夹名称)。

以下是从 CSV 数据文件中自动批量填充 PDF 表单集合的代码:

import csv
from fdfgen import forge_fdf
import os
import sys

sys.path.insert(0, os.getcwd())
filename_prefix = "NVC"
csv_file = "NVC.csv"
pdf_file = "NVC.pdf"
tmp_file = "tmp.fdf"
output_folder = './output/'

def process_csv(file):
    headers = []
    data =  []
    csv_data = csv.reader(open(file))
    for i, row in enumerate(csv_data):
      if i == 0:
        headers = row
        continue;
      field = []
      for i in range(len(headers)):
        field.append((headers[i], row[i]))
      data.append(field)
    return data

def form_fill(fields):
  fdf = forge_fdf("",fields,[],[],[])
  fdf_file = open(tmp_file,"w")
  fdf_file.write(fdf)
  fdf_file.close()
  output_file = '{0}{1} {2}.pdf'.format(output_folder, filename_prefix, fields[1][1])
  cmd = 'pdftk "{0}" fill_form "{1}" output "{2}" dont_ask'.format(pdf_file, tmp_file, output_file)
  os.system(cmd)
  os.remove(tmp_file)

data = process_csv(csv_file)
print('Generating Forms:')
print('-----------------------')
for i in data:
  if i[0][1] == 'Yes':
    continue
  print('{0} {1} created...'.format(filename_prefix, i[1][1]))
  form_fill(i)

注意:弄清楚如何定制它不应该是火箭手术。初始变量声明包含自定义配置。

在 CSV 文件的第一行中,每一列将包含 PDF 文件中相应字段名称的名称。模板中没有对应字段的任何列都将被忽略。

在 PDF 模板中,只需在您希望填充数据的位置创建可编辑字段,并确保名称与 CSV 数据匹配。

对于此特定配置,只需将此文件放在与 NVC.csv、NVC.pdf 相同的文件夹和名为“输出”的文件夹中。运行它,它会自动完成剩下的工作。

【讨论】:

  • 这很好用。我唯一需要添加的是 PDFtk 的路径:codeos.environ['PATH'] += os.pathsep + 'C:\\Program Files (x86)\\PDFtk\\bin;'
  • 我需要将 fdf_file = open(tmp_file,"w") 替换为 fdf_file = open(tmp_file,"wb") 以使其正常工作。
  • 代码运行,但我在输出 pdf 中看不到任何数据。有什么想法吗?
【解决方案2】:

替换原始文件

os.system('pdftk "original.pdf" fill_form "data.fdf" output "output.pdf"')
os.remove("data.fdf")
os.remove("original.pdf")
os.rename("output.pdf","original.pdf")

【讨论】:

  • 这可能是对上述答案的评论。
【解决方案3】:

更快的版本,不需要 pdftk 或 fdfgen,纯 Python 3.6+:

# -*- coding: utf-8 -*-

from collections import OrderedDict
from PyPDF2 import PdfFileWriter, PdfFileReader


def _getFields(obj, tree=None, retval=None, fileobj=None):
    """
    Extracts field data if this PDF contains interactive form fields.
    The *tree* and *retval* parameters are for recursive use.

    :param fileobj: A file object (usually a text file) to write
        a report to on all interactive form fields found.
    :return: A dictionary where each key is a field name, and each
        value is a :class:`Field<PyPDF2.generic.Field>` object. By
        default, the mapping name is used for keys.
    :rtype: dict, or ``None`` if form data could not be located.
    """
    fieldAttributes = {'/FT': 'Field Type', '/Parent': 'Parent', '/T': 'Field Name', '/TU': 'Alternate Field Name',
                       '/TM': 'Mapping Name', '/Ff': 'Field Flags', '/V': 'Value', '/DV': 'Default Value'}
    if retval is None:
        retval = OrderedDict()
        catalog = obj.trailer["/Root"]
        # get the AcroForm tree
        if "/AcroForm" in catalog:
            tree = catalog["/AcroForm"]
        else:
            return None
    if tree is None:
        return retval

    obj._checkKids(tree, retval, fileobj)
    for attr in fieldAttributes:
        if attr in tree:
            # Tree is a field
            obj._buildField(tree, retval, fileobj, fieldAttributes)
            break

    if "/Fields" in tree:
        fields = tree["/Fields"]
        for f in fields:
            field = f.getObject()
            obj._buildField(field, retval, fileobj, fieldAttributes)

    return retval


def get_form_fields(infile):
    infile = PdfFileReader(open(infile, 'rb'))
    fields = _getFields(infile)
    return OrderedDict((k, v.get('/V', '')) for k, v in fields.items())


def update_form_values(infile, outfile, newvals=None):
    pdf = PdfFileReader(open(infile, 'rb'))
    writer = PdfFileWriter()

    for i in range(pdf.getNumPages()):
        page = pdf.getPage(i)
        try:
            if newvals:
                writer.updatePageFormFieldValues(page, newvals)
            else:
                writer.updatePageFormFieldValues(page,
                                                 {k: f'#{i} {k}={v}'
                                                  for i, (k, v) in enumerate(get_form_fields(infile).items())
                                                  })
            writer.addPage(page)
        except Exception as e:
            print(repr(e))
            writer.addPage(page)

    with open(outfile, 'wb') as out:
        writer.write(out)


if __name__ == '__main__':
    from pprint import pprint

    pdf_file_name = '2PagesFormExample.pdf'

    pprint(get_form_fields(pdf_file_name))

    update_form_values(pdf_file_name, 'out-' + pdf_file_name)  # enumerate & fill the fields with their own names
    update_form_values(pdf_file_name, 'out2-' + pdf_file_name,
                       {'my_fieldname_1': 'My Value',
                        'my_fieldname_2': 'My Another ?alue'})  # update the form fields

【讨论】:

  • 此处显示语法错误 {k: f'#{i} {k}={v}'。使用python 3.5。是这个原因吗?
  • f 字符串需要 Python 3.6+。解决方法:{k: "#{i} {k}={v}".format(**locals())}
  • 不幸的是,在运行此脚本并将其打印出来后,似乎并未应用更改,尽管我确实在我的 Mac 上的预览中看到了这些更改。
  • 复制了完全相同的代码并更新了源文件名。它打印出所有字段,但不更新输出 pdf 文件中的任何内容。有什么建议吗?
  • 如果填充的值被隐藏并且仅在您在 Acrobat 中单击它们时才显示,请参阅讨论:github.com/mstamy2/PyPDF2/issues/355
【解决方案4】:

我写了一个基于:'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')

更多信息在这里: 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')

【讨论】:

  • 这似乎正是我想要的,但我注意到它没有填充下拉菜单,它在未来的计划中吗?谢谢!
  • @RafaelSantos 好主意,我会把它添加到未来的计划中!
猜你喜欢
  • 2014-12-13
  • 2013-07-26
  • 2017-07-26
  • 2019-02-26
  • 1970-01-01
  • 2012-11-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-10
相关资源
最近更新 更多