开发者

Populating a PDF file - Python

I have a ready-made PDF document containing placeh开发者_如何学编程older values in certain areas.

e.g. {{ first_name }}, {{ postcode }}, ...

I need to substitute these values using python.


Any suggestions?


It's a somewhat strange way to go about things, as PDFs aren't really designed to be modified. Depending on how those PDFs you have were generated, it may be very hard to do any replacement. You cannot easily alter any formatting, including line breaks, so the only case this is really useful is if you have some sort of form so you know the fields will fit.

pyPdf may allow you to extract the text, but I don't see a function to alter it while writing a second PDF. PDFedit will certainly allow you to make changes, and is scriptable, but I don't know about connecting it to Python. ReportLab only reads PDFs in the plus version, if I'm reading the page Joe Kington linked right.

I would advise reviewing why you have templates in a PDF format, and if you really do need to make changes to them from that, take a look with PDFedit - there's no telling from this description what the structure of your documents are, and it might be very hard to locate the keywords.


You could use Mako.

from mako.template import Template

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

I forgot to mention, the default for Mako templates would use ${first_name} and ${postcode} in this case. I am not sure if this can be changed.


I use reportlab to dynamically create 100 thousands of PDFs that include headers, footers, charts, tables, bullets, several different paragraph styles. etc.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜