Thursday, January 1, 2009

PythonWin 2.4.3 Create wrap classes for COM automation

This is just a little tip for those who use python and work with COM automation components. If you are a windows user,  and you want to write python scripts to interact with Adobe Photoshop, Microsoft Excel, Microsoft Visual Studio the best way to do so is using the this application's COM componts. But most of the times finding documentation this COM components registered in your Windows is tricky and difficult. Using a simple tool that is automatically installed with the pythonwin module, you can get wrap python classes for all interfaces and constants exported by a registered COM component. This usually helps a lot to find non documented methods, and even to make your python script more legible and clean.

Steps to get your wrap classes

1. Go to Start->Programs->Python->PythonWin

2. Menu Tools->COM MakePy utility->Explore and select the registered COM component you want to use.

3. The main window will show the path file where the python file has been generated

Use case with Adobe Photoshop CS3 Object Library

If you don't want to use the wrap classes, you can access the name of the active document and the free memory available of an existing instance of Adobe Photoshop using the following code:
import win32com.client

cs = win32com.client.Dispatch("Photoshop.Application")
print cs.ActiveDocument.Name
print cs.FreeMemory

The other option is to use the PythonWin App to generate the wrap classes of Adobe Photoshop CS3 Object Library and then rename the odd python file name created by PythonWin with the something like this, Then place this file in a path where your python load modules, for example the same path where you have the file of your main script and then use the following code
import adobePhotoshop

acs = adobePhotoshop.Application()
print acs.ActiveDocument.Name
print acs.FreeMemory

There is not a big improvement compared with the firs option isn't it?. The great advantage arrives when you use the wrap classes file with any python development environment like Eclipse+Pydev. During the development of your script you can use the Outline tool over the and explore the methods of any of the interfaces, for example the Application class and all its properties.

Tips to explore the wrap classes file,

1. Usually for each interface there is a wrap class with the same name, this is an empty class that is usually implemented in a class with the same name but started with underscore. For exampler, to explore the methods of the Application interface look for the implementationt in the class _Application.

2. To explore all data members of one interface explore the implementation of the _prop_map_get_ dictionary of the implementation class. For example, to explore the properties of the Application interface, look for the dictionary _prop_map_get_ of the class _Application.

Tip to find the entry point class

You will be wondering why betewen all those classes I know I have to invoke Application to access photoshop COM. Well it´s easy the entry point classes  inherits from CoClassBaseClass.

No comments:

Post a Comment