Data Mailer
I'm getting tired of writing the full path to the PDF files all the time, so let's write a function that will do it for us. See the Python documentation for more details about sys and os.path.
importsysimportos.pathdefFindTutorialFile(sFileName):sThisScriptFilePath=sys.argv[0]sTutorialDirectory=os.path.split(sThisScriptFilePath)[0]returnos.path.join(sTutorialDirectory,sFileName)
Now it's time to add the picture of the house. To do this, we have to first have the pictures prepared as PDF files. You can drag nearly any image file into Adobe Acrobat to convert it to a PDF file. Don't worry about the dimensions of the resulting PDF file, because we'll handle that all later. Anyway, the pictures are imaginatively named house1.pdf and house2.pdf.
So, let's write the basic function:
defAddHouseToPage(page,sHouseFile):doc=acrobat.documentOpen(sHouseFile)doc[0].contentsCopyToPage(page)doc.close()
The most interesting line here is:
doc[0].contentsCopyToPage(page)
This line copies the contents of the first page of the house document, containing the picture of the house, onto our template page.
So, putting everything together so far we get this whole script.
importacrobatimportsysimportos.path# FORMAT:# NAME, FEATURE1, FEATURE2, FEATURE3, FEATURE4,# FEATURE5, FEATURE6, PICTUREFILEdata=(('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'))defMakeSubsDictionary(data):dictSubs={'/N':data[0],'/F1':data[1],'/F2':data[2],'/F3':data[3],'/F4':data[4],'/F5':data[5],'/F6':data[6]}returndictSubsdefFindTutorialFile(sFileName):sThisScriptFilePath=sys.argv[0]sTutorialDirectory=os.path.split(sThisScriptFilePath)[0]returnos.path.join(sTutorialDirectory,sFileName)defAddHouseToPage(page,sHouseFile):doc=acrobat.documentOpen(sHouseFile)doc[0].contentsCopyToPage(page)doc.close()defDataMailer(sTemplateFile,tupData):doc=acrobat.documentOpen(sTemplateFile)docNew=acrobat.documentNew()docNew[:]=doc[0:1]*len(tupData)foriinrange(len(tupData)):dictSubs=MakeSubsDictionary(tupData[i])docNew[i].textrunReplace(dictSubs)AddHouseToPage(docNew[i],FindTutorialFile( tupData[i][7]))returndocNew doc=DataMailer(FindTutorialFile('template.pdf'),data)doc.forceVisible()
Now copy and run this file and look at the results. But where are the pictures of the houses? Take a closer look at the file by removing all the crop boxes, and you'll find the houses in the bottom left corner of the page.