Hack 88 Use Python from WordThis hack shows you how to create standalone Python objects that you can run from within Word using VBA. [Hack #85] showed you how to control Word from Python using COM automation [Hack #84] . Python also includes a way to create your own COM objects, which you can then use from within a Word macro.
This sample shows you how to create a COM object with a single method, SplitString. This method has semantics identical to the standard Python function string.split: the first argument is the string to split, and the second (optional) argument is the delimiter string. 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. Both of these steps are accomplished by the code in the following section. 9.10.1 The Code
The following Python code shows a small COM server: #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_ = "PythonDemo.Utilities" # NEVER copy the following ID! # Use "print pythoncom.CreateGuid( )" to make a new one _reg_clsid_ = "{40CEA5F8-4D4C-4655-BD8B-0E7B6A26B556}" def SplitString(self, val, item=None): import string if item != None: item = str(item) return string.split(str(val), item) # Add code so that when this script is run by # Python.exe, it self-registers if __name__=='__main_ _': print "Registering COM server..." import win32com.server.register win32com.server.register.UseCommandLine(PythonUtilities) Save this code as SimpleCOMServer.py. Note the following line from the script: _reg_clsid_ = "{40CEA5F8-4D4C-4655-BD8B-0E7B6A26B556}" In this line, you assign a unique identifier to your COM object. Windows uses these identifiers to keep track of the components installed on the system. Do not just copy the one from this sample into your own code. You need to create your own, which you can do easily with Python right from the DOS command line: > python >>> import pythoncom >>> print pythoncom.CreateGuid( ) This prints a new, unique identifier to the command line. Use the number created on your system in place of the sample in the code above. 9.10.2 Running the Hack
Now you'll need to register the object with COM. You can do this by executing the code as a normal Python script. From the DOS command line, type: > python SimpleCOMServer.py After running the script, you'll see the following messages: Registering COM server... Registered: PythonDemo.Utilities Now, to test the COM object from
Word, put the following macro in the
template of your choice [Hack #50]
and run it from Tools Sub PythonObj( ) Dim py As Object Dim vResponse As Variant Dim v As Variant Set py = CreateObject("PythonDemo.Utilities") vResponse = py.SplitString("Hello from Python!") For Each v In vResponse MsgBox v Next v End Sub Running this code displays three message boxes, each showing one of the words in the phrase "Hello from Python!" The default delimiter is a space, but you can also provide a delimiter string: vResponse = py.SplitString("Hello, Word", ",") To keep things tidy and help keep your registry clean, run your script again from the command line to unregister the sample COM server, but this time use the --unregister argument: > python SimpleCOMServer.py --unregister Mark Hammond |