Search code examples
pythonexcelwin32com

Python and win32com.client seem to be missing methods, in particular SetSourceData


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.)


Solution

  • 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