O'Reilly logo

Python Programming On Win32 by Mark Hammond, Andy Robinson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Automating Word

As noted previously, Word is an appealing alternative for many reasons. It has a powerful printing and formatting engine. It’s on a lot of desktops; indeed it may be a standard throughout your company. A free Word viewer is also available from Microsoft. Finally, it’s possible to post-process a document written in Word; you can generate a set of management accounts or a stack of letters, and users can add annotations as they wish.

As discussed with Excel, there are two options: use Python to take control of Word, or have Word VB grab data from Python and format it. In Chapter 9, we mentioned some guidelines for choosing which application was on top. We’ve shown Python automating Word for the following reasons:

  • There is a good chance that report production would be an automated step in the middle of the night; people won’t be using Word as an interactive query engine in the same way as Excel.

  • Python excels at formatting text and getting data into the right shape.

  • The Word document model is easy to automate. We’ll be adding chunks to the end of a document, and the details can be wrapped easily in reusable Python code.

  • Reports change and evolve rapidly. It’s easier to change a generic Python script than customize a Python COM server with new special-purpose methods.

However, there’s absolutely no technical barrier to doing it the other way around, and that may work for you in some circumstances.

As with Excel, make sure you have the Word VB reference installed to document ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required