win32com and Office
Here are some hints on using the Win32COM extensions for Python to write scripts, that use Microsoft Office Components. Thanks to Mark Hammonds excellent work, you don't need to bother with VB any longer and can automate Office from THE BEST PROGRAMMING LANGUAGE IN THE WORLD.
Here are some code examples for you
- Word 2000: How to use style formatting.
- Outlook 2000: Enumerating all folders recursive.
- Outlook 2000: Querying the default address book.
Here is a very simple example:
# This line imports the Win32COM Client-side classes. import win32com.client # This line actually creates a Word object in memory. You should be # able to see winword.exe in Taskmanager at this point. word = win32com.client.Dispatch("Word.Application") # Word is started in the background. This line brings makes its main # (empty) window visible. word.visible = 1 # This instruction creates a new Word document ! Yes, it *is* that easy. doc = word.Documents.Add() # This function writes an initial text to the document. doc.Range().Text = "Hello, world"
The COM Makepy utility
You must run the "COM Makepy" utility from the Tools menu in Pythonwin, before you can reasonably access Office objects. It will create necessary wrapper files in the folder \python21\win32com\gen_py.
One reason to do this is, that you otherwise have to manually define keywords for constants defined in TLB files. If you use Makepy, you can access all constants defined by the office objects using
For example, to get the default contacts folder from Outlook, you would write
f = mapi.GetDefaultFolder(win32com.client.constants.olFolderContacts)
So, which Librarys do you need to import ? For each major Office part you want to use, you need to import the specific library. These are named
- The Microsoft Outlook 9.0 Object Library
- The Microsoft Word 9.0 Object Library
- and so on.
Some more generic things are defined in
- The Microsoft Office 9.0 Object Library
Strings returned from COM are normally in UNICODE. If your application is ready to deal with unicode, fine. If not, you must encode the string as "Latin-1". If you do not, and you happen to have non-7Bit-Ascii characters in any of your strings, printing will not work. For example, my top-level Outlook folders contain German Umlauts. Here is what you see in the debugger:
>>> lkn u'Pers\xf6nliche Ordner' >>> print lkn Traceback (most recent call last): File "<interactive input>", line 1, in ? UnicodeError: ASCII encoding error: ordinal not in range(128)
If you want to be able to print these strings, use the following function:
DecodeUnicodeString = lambda x: codecs.latin_1_encode(x)
How do I know which objects are supported ?
If you have the MSDN installed, go to
Office Developer Documentation\ Office 2000 Documentation\ Microsoft Office 2000 Language Reference
You can find this information online at the MSDN library. If they wouldn't change their links every two weeks, I would provide a direct link, but, alas, you'll have to lookup that yourself.