PDF Snake Scripting Tutorial

 

PDF Snake home

scripting reference

table of contents

Data Mailer

Now we have to write the MakeSubsDictionary() function we used earlier. Here it is:

def
MakeSubsDictionary
(
data
)
:
dictSubs
=
{
'/N'
:
data
[
0
]
,
'/F1'
:
data
[
1
]
,
'/F2'
:
data
[
2
]
,
'/F3'
:
data
[
3
]
,
'/F4'
:
data
[
4
]
,
'/F5'
:
data
[
5
]
,
'/F6'
:
data
[
6
]
}
return
dictSubs

This creates a dictionary where the keys are all the strings that will be replaced in the template, and the elements are the data that will replace them.

So now, let's put it all together:

import
acrobat
# FORMAT:
# NAME, FEATURE1, FEATURE2, FEATURE3, FEATURE4,
# FEATURE5, FEATURE6, PICTUREFILE
data
=
(
(
'Jeffrey'
,
'3 Bedrooms'
,
'2 Bathrooms'
,
'A Swimming Pool'
,
''
,
''
,
''
,
'house1.pdf'
)
,
(
'Gary'
,
'5 Large Bedrooms'
,
'3 Bathrooms'
,
'3-Car Garage'
,
"Lot's of Shady Trees"
,
'Satelite Dish'
,
'Hot Tub'
,
'house2.pdf'
)
)
def
MakeSubsDictionary
(
data
)
:
dictSubs
=
{
'/N'
:
data
[
0
]
,
'/F1'
:
data
[
1
]
,
'/F2'
:
data
[
2
]
,
'/F3'
:
data
[
3
]
,
'/F4'
:
data
[
4
]
,
'/F5'
:
data
[
5
]
,
'/F6'
:
data
[
6
]
}
return
dictSubs
def
DataMailer
(
sTemplateFile
,
tupData
)
:
doc
=
acrobat
.
documentOpen
(
sTemplateFile
)
docNew
=
acrobat
.
documentNew
(
)
docNew
[
:
]
=
doc
[
0
:
1
]
*
len
(
tupData
)
for
i
in
range
(
len
(
tupData
)
)
:
dictSubs
=
MakeSubsDictionary
(
tupData
[
i
]
)
docNew
[
i
]
.
textrunReplace
(
dictSubs
)
return
docNew doc
=
DataMailer
(
r
'd:\pdfer\wwwroot\tutorial\template.pdf'
,
data
)
doc
.
forceVisible
(
)

As before, you'll have to change the path of template.pdf to where ever it is on your computer.

So copy this chunk into your text editor, save it to DataMailer3.py and then open it using Acrobat. You should get a result like this.

Next Page >>