In this section, we discuss how to implement COM objects using Python and a small sample of such an object. We also present some Visual Basic code that uses our Python implemented object.
For this demonstration, you’ll write a simple COM object that
supports a number of string operations. As Visual Basic is somewhat
lacking in the string-processing department where Python excels,
it’s a good candidate. The sample provides a COM object with a
. This method has semantics identical to
the standard Python function
first argument is a string to split, and the second optional argument
is a string holding the character to use to make the split. As you
have no doubt guessed, the method won’t do much more than call
There are two steps to implement COM objects in Python:
Define a Python class with the methods and properties you wish to expose.
Annotate the Python class with special attributes required by the PythonCOM framework to expose the Python class as a COM object. These annotations include information such as the objects ProgID, CLSID, and so forth.
The following code shows a small COM server written in Python:
# SimpleCOMServer.py - A sample COM server - almost as small as they come! # # We expose a single method in a Python COM object. class PythonUtilities: _public_methods_ = [ 'SplitString' ] _reg_progid_ = "PythonDemos.Utilities" # NEVER copy the following ID ...