views:

1659

answers:

4

I'm thinking that I'm going to have to run monthly reports in Crystal Reports. I've read that you can automate this with COM/ActiveX but I'm not that advanced to understand what this is or what you can even do with it.

I'm fairly familiar with Python and it looks like from what I've read, I might be able to open the report, maybe change some parameters, run it, and export it.

I also do a lot of work with Excel and it looks like you also use COM/ActiveX to interface with it.

Can someone explain how this works and maybe provide a brief example?

+2  A: 

Here's a free chapter from Python Programming on Win32 that looks promising:

Advanced Python and COM
http://oreilly.com/catalog/pythonwin32/chapter/ch12.html

Robert Harvey
+2  A: 

You can basically do the equivalent of late binding. So whatever is exposed through IDispatch is able to be consumed.

Here's some code I wrote this weekend to get an image from a twain device via Windows Image Acquisition 2.0 and put the data into something I can shove in a gtk based UI.

WIA_COM = "WIA.CommonDialog"
WIA_DEVICE_UNSPECIFIED = 0
WIA_INTENT_UNSPECIFIED = 0
WIA_BIAS_MIN_SIZE = 65536
WIA_IMG_FORMAT_PNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}"

def acquire_image_wia():
    wia = win32com.client.Dispatch(WIA_COM)
    img = wia.ShowAcquireImage(WIA_DEVICE_UNSPECIFIED,
                           WIA_INTENT_UNSPECIFIED,
                           WIA_BIAS_MIN_SIZE,
                           WIA_IMG_FORMAT_PNG,
                           False,
                           True)
    fname = str(time.time())
    img.SaveFile(fname)
    buff = gtk.gdk.pixbuf_new_from_file(fname)
    os.remove(fname)

return buff

It's not pretty but it works. I would assert it's equivalent to what you would have to write in VB.

Tom Willis
+3  A: 

First you have to install the wonderful pywin32 module.

It provides COM support. You need to run the makepy utility. It is located at C:...\Python26\Lib\site-packages\win32com\client. On Vista, it must be ran with admin rights.

This utility will show all available COM objects. You can find yours and it will generate a python wrapper for this object.

The wrapper is a python module generated in the C:...\Python26\Lib\site-packages\win32com\gen_py folder. The module contains the interface of the COM objects. The name of the file is the COM unique id. If you have many files, it is sometimes difficult to find the right one.

After that you just have to call the right interface. It is magical :)

A short example with excel (I couldn't test it because I don't have excel on my machine but I guess it works)

import win32com.client
import pythoncom

xlApp = win32com.client.Dispatch("Excel.Application")
xlApp.Visible=1

workBook = xlApp.Workbooks.Open(r"C:\MyTest.xls")
print str(workBook.ActiveSheet.Cells(i,1))
workBook.ActiveSheet.Cells(1, 1).Value = "hello"                
workBook.Close(SaveChanges=0) 
xlApp.Quit()
luc
+3  A: 

You can also find useful tips here : http://timgolden.me.uk/python/win32_how_do_i.html it's easy to adapt to any kind of application.

+1 for pointing to tim
Blauohr