15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

23.2. <strong>Programming</strong> Microsoft Office with Win32 COM<br />

One of the most useful things that you can do in an everyday business environment is to integrate<br />

support for Win32 applications. Being able to read data from and write data to such applications can<br />

often be very handy. Your department may not be necessarily be running in a Win32 environment, but<br />

chances are, your management and other project teams are. Mark Hammond's Windows Extensions for<br />

<strong>Python</strong> allows programmers to interact natively with Win32 applications in their native environment. (It<br />

can be downloaded at the book's Web site.)<br />

The Win32 programming universe is expansive. Most of it available from the Windows Extensions for<br />

<strong>Python</strong> package, i.e., Windows API, processes, Microsoft Foundation Classes (MFC) Graphical User<br />

Interface (GUI) development, Windows multithreaded programming, services, remote access, pipes,<br />

server-side COM programming, and events. And don't forget about Iron<strong>Python</strong> (http://codeplex.com/<br />

Wiki/View.aspx?ProjectName=Iron<strong>Python</strong>), an implementation of the <strong>Python</strong> language in C# for the .<br />

NET/Mono development environment. In this section, we are going to focus on one part of the Win32<br />

programming universe, which easily has practical applications for client-side, COM programming.<br />

23.2.1. Client-Side COM <strong>Programming</strong><br />

We can use Component Object Model, better known as COM (or its marketing name, ActiveX), to<br />

communicate with tools such as Outlook and Excel. For programmers, the pleasure comes with being<br />

able to "control" a native Office application directly from their <strong>Python</strong> code.<br />

Specifically, when discussing the use of a COM object, e.g., launching of an application and allowing<br />

code to access methods and data of that applications, this is referred to as COM client-side<br />

programming. Server-side COM programming is where you are implementing a COM object for clients to<br />

access.<br />

<strong>Core</strong> Note: <strong>Python</strong> and Microsoft COM (Client-Side) <strong>Programming</strong><br />

<strong>Python</strong> on the Windows 32-bit platform contains connectivity to COM,<br />

a Microsoft interfacing technology that allows objects to talk to one<br />

another, or more higher-level applications to talk to one another,<br />

without any language- or format-dependence. We will see in this<br />

section how the combination of <strong>Python</strong> and COM (client programming)<br />

presents a unique opportunity to create scripts that can communicate<br />

directly with Microsoft Office applications such as Word, Excel,<br />

PowerPoint, and Outlook.<br />

The prerequisites to this section include running on a Win32 platform with both <strong>Python</strong> and the Windows<br />

Extensions for <strong>Python</strong> installed. You must also have one or more Microsoft applications available to try<br />

the examples with. The download instructions for the Windows Extensions should be adequate to get<br />

your system ready to go. Since <strong>Python</strong>Win comes with the Extensions distribution, we recommend IDE<br />

for building and testing your Win32 scripts.<br />

In this section, we will show you how you can interact with an Office application. We will present a few<br />

examples, some of them quite useful, and describe how they work in detail. You will also find several of<br />

these at the "<strong>Python</strong> Cookbook" Web site. We confess to readers that we are not COM or VisualBasic<br />

experts and we are well aware that our examples can be vastly improved. We would like to solicit the<br />

readership to drop us a line and send us any comments, suggestions, or improvements that you would

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!