In the previous sections I introduced the basic requirements for creating a COM server in Python. Now let's put the concepts into practice. The example will provide a simple functionality -- splitting a given string. There are two parts of the application:
Let's start with the server. First let's define the Python class contained in PyCOMServer.py - PythonUtilities class PythonUtilities: This class defines a single method, SplitString, that takes two arguments: item, which is the string to be split; and the value contained in val on the basis of which string has to be split. The next step is to embed the attributes so that the class can expose its functionalities through the COM: class PythonUtilities: def SplitString(self, val, item=None): Since there is only one method that needs to be exposed, the list for _public_methods_ contains only the SplitString method. Next, the name by which it can be called is given via _reg_progid_ which is PythonServer.Utilities. Finally the class id generated using pythoncom.CreateGuid(). Next is the main code that is required to register and run the COM server. class PythonUtilities: def SplitString(self, val, item=None): # Add code so that when this script is run by Next comes the client. For developing the client start the Macro editor either in MS Word or MS Excel. Enter the name for the macro. The implementation of the macro is as follows: Set PythonUtils = CreateObject("PythonDemos.Utilities") That brings us to the end of this discussion. The application developed doesn't apply the design patterns, as certain advanced aspects of COM programming have to be covered yet. That's the agenda for the next part: advanced aspects of COM servers. Till then…
blog comments powered by Disqus |