Driving Excel from Python in Windows
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:
x:> makepy.py "Microsoft Excel 11.0 Object Library"...Generating...Importing modulex:> ipython
> from win32com.client import Dispatch> excel = Dispatch("Excel.Application")> wb = excel.Workbooks.Append()> range = wb.Sheets[0].Range("A1")> range.[Press Tab]range.Activate range.Mergerange.AddComment range.NavigateArrowrange.AdvancedFilter range.NoteText...range.GetOffset range.__repr__range.GetResize range.__setattr__range.GetValue range.__str__range.Get_Default range.__unicode__range.GoalSeek range._get_good_object_range.Group range._get_good_single_object_range.Insert range._oleobj_range.InsertIndent range._prop_map_get_range.Item range._prop_map_put_range.Justify range.coclass_clsidrange.ListNames range.__class__> range.Value = 32...
Documentation links:
- The O'Reilly book Python Programming on Win32 has an Integrating with Excel chapter.
- Same book, free sample chapter Advanced Python and COM covers makepy in detail.
- Tutorials
- win32com documentation, I suggest you read this first.
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.
from win32com.client import DispatchxlApp = Dispatch("Excel.Application")xlApp.Visible = 1xlApp.Workbooks.Add()xlApp.ActiveSheet.Cells(1,1).Value = 'Python Rules!'xlApp.ActiveWorkbook.ActiveSheet.Cells(1,2).Value = 'Python Rules 2!'xlApp.ActiveWorkbook.Close(SaveChanges=0) # see note 1xlApp.Quit()xlApp.Visible = 0 # see note 2del xlApp
Good luck!