We have various spreadsheets that employ deliciously complicated macros and third party extensions to produce complicated models. I'm working on a project that involves slightly tweaking various inputs and seeing the results. Rather than doing this by hand or writing VBA, I'd like to see if I can write a python script to drive this. In other words, the python script will start up, load the excel sheet, and then interact with the sheet by making minor changes in some cells and seeing how they affect other cells.
So, my question is twofold:
What is the best library to use to drive excel from python in such fashion?
Where's the best documentation/examples on using said library?
Cheers, /YGA
I've spent about a week, and here's my implementation (or rather proof-of-concept): https://github.com/Winand/SimplePython
Main idea is to make Python code as easy to write and call from Microsoft Office as VBA macros (i've made Add-In only for Excel).
There is an interesting project for integrating python into excel as an in-process DLL which can be fount at:
http://opentradingsystem.com/PythonForExcel/main.html
Another somewhat more simple project along the same idea exists at:
http://www.codeproject.com/Articles/639887/Calling-Python-code-from-Excel-with-ExcelPython
These projects seem to have a lot of promise but need more development.
I've done this by using pywin32. It's not a particularly pleasant experience, since there's not really any abstraction; it's like using VBA, but with python syntax. You can't rely on docstrings, so you'll want to have the MSDN Excel reference handy (http://msdn.microsoft.com/en-us/library/aa220733.aspx is what I used, if I remember correctly. You should be able to find the Excel 2007 docs if you dig around a bit.).
See here for a simple example.
Good luck!
For controlling Excel, use pywin32, like @igowen suggests.
Note that it is possible to use static dispatch. Use
makepy.py
from the pywin32 project to create a python module with the python wrappers. Using the generated wrappers simplifies development, since for instance ipython gives you tab completion and help during development.Static dispatch example:
Documentation links:
I wrote python class that allows working with Excel via COM interface in Windows http://sourceforge.net/projects/excelcomforpython/
The class uses win32com to interact with Excel. You can use class directly or use it as example. A lot of options implemented like array formulas, conditional formatting, charts etc.
Check out the DataNitro project (previous name IronSpread). It is a Python plug-in for Excel.