Python Excel图表typespywintypes.com_error

我的代码的以下片段正常工作。 它创build一个图表并将其设置为xlLine

chart = ws.Shapes.AddChart().Select() xl.ActiveChart.ChartType = win32com.client.constants.xlLine xl.ActiveChart.SetSourceData(Source=ws.Range(range)) 

但是,如果我运行这个代码

 chart = ws.Shapes.AddChart().Select() xl.ActiveChart.ChartType = win32com.client.constants.xlColumn xl.ActiveChart.SetSourceData(Source=ws.Range(range)) 

我得到以下错误

 Traceback (most recent call last): File "C:\Users\Simon\workspace\python\pyexcelchart\pyexcelchart.py", line 52, in <module> excelChart(workbook=wbk,worksheet="Sheet1",range="A1:B6") File "C:\Users\Simon\workspace\python\pyexcelchart\pyexcelchart.py", line 46, in excelChart xl.ActiveChart.ChartType = win32com.client.constants.xlColumn File "C:\WinPython-32bit-2.7.3.3\python-2.7.3\lib\site-packages\win32com\client\__init__.py", line 512, in __setattr__ d.__setattr__(attr, value) File "C:\WinPython-32bit-2.7.3.3\python-2.7.3\lib\site-packages\win32com\client\__init__.py", line 474, in __setattr__ self._oleobj_.Invoke(*(args + (value,) + defArgs)) pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147467259), None) 

我已经运行makepy.py并生成Excel COM常量,但即使我使用ChartType = 3等数字types,也会得到相同的错误。 如果我设置ChartType = 4(这是线)它工作得很好。

我注意到,如果我将ChartType xlColumn设置为xlRandomText,我得到一个干净的AttributeError:xlRandomText。 所以似乎python是好的xlColumn,但有问题自动化,除了xlLine的任何东西。 我不知道这是一个Python / Excel 2010的问题? 顺便说一句,我使用Python 2.7和Excel 2010

我不知道为什么,但xlColumn没有列为可用的图表types在这里: http : //msdn.microsoft.com/en-us/library/office/bb241008( v=office.12) .aspx

但是,这可能适用于您:

 xl.ActiveChart.Type = win32com.client.constants.xlColumn