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: ,

Tuesday, May 01, 2007

I'm SmallTalk

You are Smalltalk. You like to treat everyone the same way, but this lack of individuality makes everyone feel like objects.
Which Programming Language are You?

Thursday, April 20, 2006

TrypBlog

I am in the process of creating a small webapp in TurboGears that I am calling TrypBlog, pronounced "TripBlog". Its purpose is quite simple to allow me and my family to keep trip logs on our vacation this summer.

Nothing fancy just something small and functional, and since I am using the identity feature built into Turbogears I will hopefully learn some things :)

So far about all I have done is create the admin user, I am not working on the page to create new users. Again nothing fancy, but something that needs to be done.

This is my Python Blog

I decided that I need to separate my Python stuff from my regular blog. So what follows are my attempts at documenting my forrays into the Python Programming language.