Join Club ShowMeDo to Learn Python!

This series is a part of Club ShowMeDo. Click to learn why you should join our club with a simple monthly subscription.

  • We teach Python and all the cool modules
  • Topics include beginner Python, GUIs, Web and Desktop Apps
  • Long, specially crafted video tutorials just for you
  • Club video tutorials extend our normal Free content
  • Save your time - we've done the research for you
  • Keep your skills up-to-date
  • Learn at your own pace, everything you need is shown to you
  • Over 12 hours of archived material are waiting for you in the club

Name: [002] Ian Ozsvald
Member: 105 months
Authored: 181 videos
Description: I am the co-founder of ShowMeDo (see http://showmedo.com/about), author of `The Screencasting Handbook <http://thescreencastinghandbook.com>`_ and the founder of the professional screencast production company `ProCasts <http://procasts.co.uk>`_: .. image:: http://procasts.co.uk/media/procasts_sma ...

A COM Server in Python in 20 Lines [ID:623] (6/6)

in series: COM and Python with pyWin32

(Showmedo is undergoing major changes. To report any problems viewing the videos please email us and include browser and OS specifics. Cheers - Kyran.)

Finally let's look at building a COM server in Python which is called from within Excel. We'll develop a 'hello' function and a more complex Factorial function using pyWin32's COM interface.

class ShowMeDoDemo:
    _public_methods_ = [ 'Factorial', 'Hello' ]
    _reg_progid_ = "ShowMeDo.Demo"
    # use
    # import pythoncom
    # print pythoncom.CreateGuid()
    # to make a GUID as they are machine-specific
    _reg_clsid_ = "" # e.g. "{4D93DCBC-DE99-4D5A-ACFC-44F82BDB9889}"
    
    def Hello(self):
        "say Hello"
        return "Hello World!"
    
    def Factorial(self, n):
        "calculate n!"
        result = 1
        for value in range(2,n+1):
            result = result * value
        #1/0 # generates a ZeroDivision exception
        return result
    
if __name__ == "__main__":
    # use 'python com.py' to register the COM server
    # use 'python com.py --unregister' to unregister it
    print "Registering COM server..."
    import win32com.server.register
    win32com.server.register.UseCommandLine(ShowMeDoDemo)
    


#In [2]: import pythoncom
#In [3]: pythoncom.CreateGuid()
#Out[3]: IID('{4D93DCBC-DE99-4D5A-ACFC-44F82BDB9889}')

#C:\pyWin32_tutorial>python com.py
#Registering COM server...
#Registered: ShowMeDo.Demo
    
    
# In Excel
# Alt-F8 brings up the Macros dialog
# Give it a name of 'Test', press Create
#Make the code look like:
#Sub Test()
    #Set Demo = CreateObject("ShowMeDo.Demo")
    #MsgBox Demo.Hello()
    #response = Demo.Factorial(1)
    #MsgBox response
#End Sub
# Use F5 to execute it

# To develop code, we can edit live
# but we have to close the VBA editor and Excel each time
# we don't need to re-register (or unreg then reg) our COM server

Got any questions?

Get answers in the ShowMeDo Learners Google Group.

Video statistics:

  • Video's rank shown in the most popular listing
  • Video plays: 103 (since July 30th)
  • Plays in last week: 0
  • Published: 77 months ago

Thank-yous, questions and comments

If this video tutorial was helpful please take some time to say thank-you to the authors for their hard work. Feel free to ask questions. Let the author know why their video tutorial was useful - what are you learning about? Did the video tutorial save you time? Would you like to see more?

You may also want to see our ShowMeDo Google Group to speak to our active users and authors.

Your email address will not be published.

Show some quick comments >>








All comments excluding tick-boxed quick-comments

EXCELlent introduction to using COM with Python. This will be handy in some projects I am looking to do.

Thank you for all the effort you put into making these videos.


Good series on COM. Will come in handy when working with Excel. Thanks


Ian,

This is a great video! This kind of topic is so useful to a newbie Python programmer. Thanks for making this look so easy to do.


Review of A COM Server in Python in 20 Lines

Now this is awesome and exactly what i have been looking for...

Nice job!!!


3. anonymous Fri, 13 Jun 2008 16:35

Extremely useful!!

One question though: if I want to have a function take multiple parameters (say two ranges) what is the correct syntax in Python & Excel. I've tried what I thought would be the right extension, but I keep getting Value errors in Excel.


Great stuff! Too bad user definde functions, i.e. functions live in the spreadsheet, do not work like thius. Oh well, they are a hassle to implement via Visual Studio Tools for Office so this will certainly suffice. Cool stuff.


Video published, thanks for contributing to ShowMeDo


Showmedo is a peer-produced video-tutorials and screencasts site for free and open-source software (FOSS)- with the exception of some club videos, the large majority are free to watch and download.

how to help » about » faq »

Educating the Open-source Community With Showmedo

Although as important as the software it supports, education and documentation are relatively neglected in the Open-source world. Coders love to code, and explaining how best to use or improve the software tends to be deferred or even sidelined.

At Showmedo we believe the community can play a vital role here and also say thanks for the tools and software that make our lives easier. If you have a piece of software you love or a programming langugage you are enthusiastic about, why not make a screencast showing others how to use it? All the stuff you wish you'd been told, the tips, tricks, insights that would have saved you time and frustration.

Screencasting is easier than you think, and we're happy to help you. You can emailus for advice or just use some of the how-to screencasts on the site. This screencasting learning-pathis a good place to start.

Kudos and Thanks for Ian

Content

Feedback

Showmedo's development is fairly rapid and bugs will inevitably creep in. If you have any problems please drop us a line using the contact address below. Likewise, any suggestions for improvements to the site are gratefully received.

feedback@showmedo.com