• Could not connect to Twitter

Using pyWin32 with Excel

I’ve started another Club series to show you how to use pyWin32 on Windows to talk to COM-aware applications like Excel.

Personally I’ve used Excel as a simple charting tool for quick science research. I figured that several screencasts on this leading to the creation of a Python COM server would make for a good introduction. The COM server can be used to embed Python code into other applications.

The 3 minute overview gives you an introduction to what’s covered. For the first two episodes I talk about using pyWin32 to talk interactively to Excel and fixing some weird behaviour with local references that’ll catch you out when you start experimenting.

The weird behaviour doesn’t cleanly close a previous Excel instance so you can’t start another one – you have to delete the process in TaskManager each time if you don’t know how to close things down nicely.

Over the next 10 days I’ll release the next three episodes covering:

  • using Excel for charting
  • using Excel for input as well as output
  • building a Python COM server which we call from within Excel

This is one of our ShowMeDo Club tutorials for subscribers, a part of our 231 (mostly free!) screencasts that help you to learn Python. Subscribers get access to 6+ hours of Python tutorials, exercises and walk-throughs with new episodes every week.

Please share:
  • DZone
  • del.icio.us
  • Reddit
  • Furl
  • Ma.gnolia
  • email
  • StumbleUpon
  • Technorati
  • TwitThis
  • Slashdot
If you enjoyed this post, make sure you subscribe to my RSS feed!

Related posts:

  1. COM and Python with pyWin32
  2. Introduction to Python web-programming: CGI
  3. Starting with twill, nosetests and easy_install
  4. New Club series for Python Beginners – Common Variables and Containers
  5. “What does Python look like?” series finished in Club ShowMeDo

1 comment to Using pyWin32 with Excel

  • ggivler

    I have not had a chance to watch the demo, but here is something I learned doing this.

    I do some spreadsheet creation here at work for various departments, so I use a brute force method of finding all instances of EXCEL and killing them, I warn the user about this when the program starts. My programs all have a GUI so they can say no, then shut there excel stuff down before running my program. Here is the code:

    import wmi

    def CloseXL(self):
    try:
    if self.xl is not None:
    self.xlBook.Close(SaveChanges=0)
    self.xl.Quit()
    del self.xl
    except AttributeError, err:
    if err[0] != ‘xl’:
    raise AttributeError, err

    c = wmi.WMI()
    if not self.NoExcel:
    for process in c.Win32_Process(name=”excel.exe”):
    process.Terminate()