For Python you will need fdfgen lib and pdftk
@Hugh Bothwell's comment is 100% correct, so I will continue this answer with a working implementation.
If you are in windows, you also need to make sure that both python and pdftk are contained in the system path (if you do not want to use long folder names).
Here is the code for automatic loading - it fills the collection of PDF forms from the CSV data file:
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)
Note. It doesn't have to be rocket surgery to figure out how to set it up. Initial variable declarations contain user configuration.
In CSV, in the first row, each column will contain the name of the corresponding field name in the PDF file. Any columns that do not have corresponding fields in the template will be ignored.
In the PDF template, simply create editable fields in which you want your data to be populated, and make sure the names match the CSV data.
For this specific configuration, simply place this file in the same folder as your NVC.csv, NVC.pdf, and a folder named "output". Run it and it will automatically do the rest.
Evan plaice
source share