4

I'm working on using Python to automate some Excel data analysis. I have most of the basics down, but I can't seem to find any way to call SetSourceData or SeriesCollection successfully. I've read this post (and all the others) and found it hopeful, but it doesn't seem to match my experience. I'm using Python 2.7.3, 32 bit on a Windows machine.

Has anyone successfully used SetSourceData or SeriesCollection?

Here's a simplified version of the code I'm using and the error:

chart = chartSheet.ChartObjects(1)

chart.SetSourceData(chartSheet.Range("A1:B2"),PlotBy=2) 

I specify PlotBy because of this, but I doubt it matters

And the error:

raise AttributeError("'%s' object has no attribute '%s'" % (repr(self), attr))

AttributeError: '<win32com.gen_py.Microsoft Excel 14.0 Object Library.ChartObject instance at 0x68557120>' object has no attribute 'SetSourceData'

ARGH. Should I look into IronPython? (I've found a few other things that are specified in the MSDN documentation but don't seem to work in Python... but can't remember them now.)

Community
  • 1
  • 1
Robert Balicki
  • 1,583
  • 2
  • 16
  • 24
  • `win32com` is great, but the [python excel](http://www.python-excel.org/) modules might make life easier for you.. – danodonovan May 01 '13 at 10:03
  • Actually, I use both. To the extent I can, I use the Python excel modules, but one of my main goals is the creation of a "paper trail" of Excel objects (namely charts and tables) that non-programmers can view and edit, when the program falls short. – Robert Balicki May 03 '13 at 05:09

1 Answers1

2

This line

chart = chartSheet.ChartObjects(1)

doesn't actually return a Chart object (quote)

This method is not equivalent to the Charts property. This method returns embedded charts; the Charts property returns chart sheets. Use the Chart property to return the Chart object for an embedded chart.

I haven't tried this, but it sounds as if you need to reference the chart attribute, maybe like this:

chartObject = chartSheet.ChartObjects(1)
chart = chartObject.chart
danodonovan
  • 19,636
  • 10
  • 70
  • 78