Interacting with other Windoze apps

brueckd at tbye.com brueckd at tbye.com
Mon Apr 22 18:15:31 EDT 2002


On Mon, 22 Apr 2002, Johnson, Brad wrote:

> I'm a new Python programmer and have a question about "interacting" with
> other Windoze applications.  I'd like to send key strokes/characters to an
> application window, but have not been able to find any documentation on
> this.  Is it possible?  For example, suppose a user has Microsoft Word open.
> Is it possible to send an "Alt + F" then an "O" to the Word window to
> activate the *F*ile menu and select *O*pen?
>
> Any pointers (and sample code!) would be appreciated.  Thanks.

Hi Brad, Here's two ways to do this, and both should be possible from
Windows, but I've only tried one of them. In both cases you need the win32
Python extensions by Mark Hammond, which you already have if you are using
the ActiveState distribution of Python.

(1) (the more hacky way) If you're familiar with Win32 programming, you
can probably solve this problem the same way you would from C (use the
win32 API to find the correct window handle and then send it the correct
message. I don't have any sample code to do this, but your Python code
will follow the C equivalent almost call-for-call on the API level (the
Python extensions are wrappers for the equivalent win32 calls so the names
are usually very close, if not identical, to the originals).

(2) If the application you want to interact with exposes a COM interface
(Word falls into this category), then you can use the win32com package of
Python extensions (part of the same win32 package mentioned above) to
automate it (so you could then tell Word to open a new document, write
stuff stuff, save it, etc). The Python section of www.faqts.com has an
example, you can also search on Google groups for
'win32com.client.Dispatch'.

-Dave






More information about the Python-list mailing list