Coding in the D and other interesting things
In this post I am going to quickly explain how to create Excel formulas that call class methods that are written completely in Python. I will be using the rottenTom class created in my previous post to create some Excel formulas such as ‘=getyear(“space jam”)’ and have it return 1996 in the Cell.
To do this I use pywin32 and register my rottenTom class as a COM server. COM is Microsoft’s way of exposing interfaces for code that are language agnostic. This allows you to create a Python class and instantiate it within VBA, as we will see in this example. If you have not worked with COM before, Activestate has a good overview of setting up a COM server and Mark Hammond (the guy you thank at the end of every Python install on Windows) has an old but still very pertinent book on Python on Windows. Chapter 12 is specifically dedicated to Python and COM.