This book is about using Python to get jobs done on Windows.
We hope by now you have heard of Python, the exciting object-oriented scripting language that is rapidly entering the programming mainstream. Although Python is perhaps better known on the Unix platform, it offers a superb degree of integration with the Windows environment. One of us, Mark Hammond, is responsible for many of Python's Windows extensions and has coauthored the Python COM support, both of which are major topics of this book. This book can thus be considered the definitive reference to date for Python on the Windows platform.
This is intended to be a practical book focused on tasks. It doesn't aim to teach Python programming, although we do provide a brief tutorial. Instead, it aims to cover:
How Python works on Windows
The key integration technologies supported by Python on Windows, such as the Win32 extensions, which let you call the Windows API, and the support for COM
Examples in many topic areas showing what Python can do and how to put it to work
In the end, we hope you will have a clear idea of what Python can do and how to put it to work on real-world tasks.
We expect this book to be of interest to two groups of people:
Windows developers, administrators, and IT managers
You may be an experienced Windows developer using C++, Visual Basic, Delphi or other development tools, or you may be involved in managing information technology (IT) and need to make decisions as to the right tools for large projects. No doubt you hear about several new languages every year and meet zealots who insist that each is the ultimate development tool. You've perhaps heard a colleague talking about Python or read a few articles about it and are curious why people rave about it. By the end of this book, you should know!
Python converts from Unix
Python is one of the major products of the Open Source revolution (http://opensource.org/) and has a large following on Unix platforms. There are a large number of Python users within the Unix tradition who are forced, with varying degrees of resistance, to work in a Windows environment. We hope to open your eyes. Most of the things you do on Unix can be done on Windows, and Windows offers exciting programming possibilities.
Readers may vary considerably in their programming experience. We don't aim to teach the language systematically and assume you are familiar with other programming languages. Someone familiar with Visual Basic, for example, should be able to follow most of the book. However, some sections regarding Windows internals or C integration assume C or C++ familiarity.
We assume a fairly typical business-computing platform with Windows NT 4.0, Microsoft Office, access to the Internet or an internal TCP/IP network, and adequate memory to run smoothly. Python is equally happy on Windows 95 and 98, and we have tried to comment on the differences as they occur.
The book is broken into three parts. The first part is mainly introductory in nature and sets the framework for the other two sections. The second section focuses on building an advanced Windows application using Python. The main purpose of this application is to show some possibilities for your applications. The third section provides a Python on Windows cookbook.
This part covers the basics about the language and the platform and should be read by everyone not familiar with using Python on Windows.
Chapter 1, What Is Python?
This is a Python primer: what it's good for, who's using it for what, and where to get hold of it. If you want to brief your manager on Python, show her this chapter!
Chapter 2, Python Language Review
This chapter is a high-speed minitutorial; it should at least enable you to follow the code examples. We don't aim to teach Python from scratch, but instead point you to the right places for more detailed tutorials.
Chapter 3, Python on Windows
Here we cover how Python is set up on Windows and review some of the Windows-specific packages and extensions.
Chapter 4, Integrated Development Environments for Python
Chapter 4 covers PythonWin and IDLE, two integrated development environments for Python, each complete with syntax coloring, object browsers, and debuggers.
Chapter 5, Introduction to COM
Here we introduce Python's support for COM, Microsoft's key integration technology.
This part is an in-depth exploration of what you can do with a Python application on Windows. ''Advanced'' refers to the capabilities Python gives to your programs, not to the level of programming difficulty. Although the focus is on COM, it brings many features of the language together. In some cases, we use features not fully explained until later chapters; we want to make you say "Wow, so you can do that," rather than "Wow, so that's how you do it": we save that revelation for the last section. The project is to develop an extensible accounting toolkit in Python. Part II, like Part I, is intended to be read straight through.
Chapter 6, A Financial Modeling Toolkit in Python
This chapter explains the business requirements of our application and develops a Python class library that encapsulates the basic rules followed by accounting systems. For people new to Python, this provides a more in-depth set of examples of object-oriented programming with Python.
Chapter 7, Building a GUI with COM
Here we show how to build a COM server that exposes our Python "engine" to other applications, building a fairly standard user interface in Visual Basic on top our application. In the process, we cover how to move data of all types back and forth between Python and other languages using COM.
Chapter 8, Adding a Macro Language
Next we show how to expose the Python interpreter to end users. Our application allows users to work with a command prompt, write scripts, and even define new event handlers and graphical views, thereby making the application extensible. Python makes this capability, normally found only in large commercial applications, child's play.
Chapter 9, Integration with Excel
This chapter shows the other side of the coin: using Python as a COM object to acquire data from and send data to Excel.
Chapter 10, Printed Output
Here we cover a range of techniques for printing and for producing reports in general, including direct printer control, automating Microsoft Word, and direct generation of financial reports in PDF format.
Chapter 11, Distributing Our Application
Finally, we show how COM makes it extremely easy to run the Python engine on one machine and the client interface on another.
Each chapter in this section may be taken in isolation and covers one particular area of interest in detail. The focus is task-based, and we look at various technologies and libraries in each section, concentrating on how to get jobs done with Python. These chapters naturally vary in their technical depth and appeal, but we hope that there will be plenty of interest for everyone.
Chapter 12, Advanced Python and COM
This is the definitive reference on Python's support for COM.
Chapter 13, Databases
This chapter shows how to connect to databases from Python and illustrates how to manipulate data.
Chapter 14, Working with Email
Here we take a look at some common techniques for dealing with email on Windows.
Chapter 15, Using the Basic Internet Protocols
This is a brief discussion on how to use common Internet protocols from Python on Windows.
Chapter 16, Windows NT Administration
In this chapter, we discuss the language extensions for working with users, groups, drives, shares, servers, and so forth.
Chapter 17, Processes and Files
This chapter presents Python's facilities for working with processes and files, both in a portable and a Windows-specific way.
Chapter 18, Windows NT Services
Here we explore Python's thorough support for Windows NT services.
Chapter 19, Communications
This chapter discusses serial I/O, remote access services, and TCP/IP sockets.
Chapter 20, GUI Development
Here we present three toolkits: PythonWin, Tkinter, and wxPython.
Chapter 21, Active Scripting
This chapter presents a complete guide to Python's extensions to support Microsoft's Active Scripting technology.
Chapter 22, Extending and Embedding with Visual C++ and Delphi
As the name implies, this chapter explains how you can extend Python's capabilities using other languages.
Appendix A, Key Python Modules and Functions
This appendix is a short reference to the most important modules and functions in Python.
Appendix B, Win32 Extensions Reference
This appendix is a brief reference to the SDK functions and objects presented in this book.
Appendix C, The Python Database API Version 2.0
This appendix is a reproduction of the specification.
Appendix D, Threads
This appendix is limited to Windows-specific threading issues.
All examples in this book have been tested with Python 1.5.2, but should work as later versions of Python are released.
Many examples are so small they can be typed interactively into the Python interpreter. In these cases, the examples always show the Python prompts and responses:
>>> print "This is interactive source code"
This is interactive source code
All example code presented as source files are available for download from the authors' web page, http://starship.python.net/crew/mhammond/ppw32/. The source code is available as a ZIP file organized by chapter. See the web page and the README.TXT file in the ZIP file for more details.
This book-uses the following typographical conventions:
Introduces new terms and indicates URLS, variables or user-defined files and directories, programs, file extensions, filenames, hostnames, directory or folder names, and UNC pathnames.
Indicates Python language elements, command-line computer output, code examples, commands, keywords, functions, modules, classes, interfaces, instances, collections, objects, properties, methods, packages, and constants.
Constant width italic
Indicates placeholder names in syntax specifications and registry keys.
|The owl icon designates a note, which is an important aside to the nearby text.|
|The turkey icon designates a warning relating to the nearby text.|
Please address comments and questions concerning this book to the publisher:
O'Reilly & Associates
101 Morris Street
Sebastopol, CA 95472
1-800-998-9938 (in United States or Canada)
1-707-829-0515 (international or local)
You can also send us messages electronically. To be put on the mailing list or request a catalog, send email to firstname.lastname@example.org.
To ask technical questions or comment on the book, send email to email@example.com.
We have a web site for the book, where we list errata and any plans for future editions. You can find it at http://www.oreilly.com/catalog/pythonwin32/.
For more information about this book and others, see the O'Reilly web site http://www.oreilly.com.
Although we were warned about what it takes to write a book like this, we were still not prepared! Many people contributed to this book and assisted with the enormous workload it involved.
The support of Guido van Rossum, the author of the Python language, for the Windows version of Python has enabled it to develop into a serious contender for most Windows development work. Guido somehow manages to balance the opposing forces of maintaining platform independence and allowing Python to take advantage of the platform's unique features.
The wonderful Python community also has itself to thank for allowing this book to come to fruition. The Python newsgroup seems to be one of the final bastions of Usenet where spams are ignored, newbies are treated kindly, and trolls are either ignored or quickly turned into a serious and on-topic discussion. Many smart and friendly people hang out on the newsgroup, and it's one of the more esoteric reasons many people find using the language enjoyable.
The following people have provided invaluable help, whether with full reviews, in-depth knowledge of particular areas, or informal feedback: Guido van Rossum, Gordon McMillan, Greg Stein, Marc-André Lemburg, Christian Tismer, Gary Herron, Robin Dunn, Mike Da Silva, Richard Kennedy, and Damien Watkins. Many others too numerous to mention, have played a part in shaping the book, whether in direct correspondence with us or in developing and working with the packages in this book.
At O'Reilly, we'd like to thank Robert Denn, Steven Abrams, and probably a whole bunch of other people we have never heard of.
Most important of all, Mark and Andy both wish to thank their families for their support and incredible patience, and they promise not to do this again any time soon.