Thursday, June 14, 2007

Calling Adobe Distiller from Python

Here is something that is simple to do, but took me days to figure out.

There is a book that I picked up a while back called Python Programming on Win32 from O'Reilly. It has been very useful when using Python to programmatically create Excel Spreadsheets, which has been useful when writing quick and dirty programs here at work.

However I had the need to feed a postscript file that I was parsing to Adobe Distiller and wanted to do it within my python program that was doing the parsing of the Postscript. Searching the web was less than useful so here is what I found.

Using makepy.py which comes with Pythonwin, I was able to create a module based on the classID of Adobe Distiller, then you can write some very simple code to get this to work.

The important thing I needed to know was that Adobe registers the Distiller OLE object as a none intuitive PdfDistiller.PdfDistiller.1

Also I put it in a try: except: block however the OLE object when it throws a error is rather unfulfilling in its error message giving some large negative number and text that reads basically an error has occurred.

Anyway, here is the code, just in case you aren't sure that large number in the brackets as a parameter of gencache.EnsureModule is the CLSID of Adobe Distiller:
import os
import sys
import win32com.client
from win32com.client import gencache

try:
  gencache.EnsureModule('{317DA881-ECC5-11D1-B976-00600802DB86}', 0, 1, 2)
  distllr = win32com.client.Dispatch('PdfDistiller.PdfDistiller.1')
  distllr.FileToPDF(postscriptfilename,pdffilname,adobejoboptionsfile)
except Exception, e:
  print e


If you need a more thorough example let me know. I can provide you with something more detailed.

Enjoy :-)

Labels: ,

0 Comments:

Post a Comment

<< Home