Filling a PDF file - Python

I have a ready-made PDF document containing placeholder values ​​in certain areas.

eg. {{first_name}}, {{postcode}}, ...

I need to substitute these values ​​with python.


Any suggestions?

+7
source share
3 answers

This is a somewhat weird way of doing things, since PDF files are not meant to be modified. Depending on how these PDF files were created, it can be very difficult to perform any replacement. You cannot easily change any formatting, including line breaks, so the only case that is really useful is if you have some form so that you know that the fields will match.

pyPdf may allow you to extract text, but I don’t see a function to change it while writing a second PDF. PDFedit will certainly allow you to make changes and be scriptable , but I don’t know how to connect it to Python. ReportLab only reads PDF files in the plus version if I read the page Joe Kington is right on.

I would advise you to consider why you have PDF templates, and if you really need to make changes to them, look with PDFedit - there is no description from this description, what is the structure of your documents, and it can be very difficult to find keywords.

+4
source

You can use Mako .

from mako.template import Template template = Template(filename='template.pdf') output = Template.render(first_name='Simon', postcode='12345') 

I forgot to mention that by default Maco templates will use $ {first_name} and $ {postcode} in this case. I'm not sure if this can be changed.

+3
source

I use reportlab to dynamically create 100 thousand PDF files containing headers, footers, charts, tables, bullets, several different paragraph styles. etc.

http://www.reportlab.com/software/opensource/

0
source

All Articles