Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Excel图表类型pywintypes.com_错误_Python_Excel_Charts_Win32com - Fatal编程技术网

Python Excel图表类型pywintypes.com_错误

Python Excel图表类型pywintypes.com_错误,python,excel,charts,win32com,Python,Excel,Charts,Win32com,下面的代码片段运行良好。它创建图表并将其类型设置为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.C

下面的代码片段运行良好。它创建图表并将其类型设置为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)
回溯(最近一次呼叫最后一次):
文件“C:\Users\Simon\workspace\python\pyexcelchart\pyexcelchart.py”,第52行,在
Excel图表(工作簿=wbk,工作表=“Sheet1”,范围=“A1:B6”)
文件“C:\Users\Simon\workspace\python\pyexcelchart\pyexcelchart.py”,第46行,在Excel图表中
xl.ActiveChart.ChartType=win32com.client.constants.xlColumn
文件“C:\WinPython-32bit-2.7.3.3\python-2.7.3\lib\site packages\win32com\client\\uuuuuuu init\uuuuuuu.py”,第512行,位于\uuuu setattr中__
d、 设置属性(属性,值)
文件“C:\WinPython-32bit-2.7.3.3\python-2.7.3\lib\site packages\win32com\client\\uuuuuuuuu init\uuuuuuuuu.py”,第474行,位于\uuuu setattr中__
self._oleobj_.Invoke(*(args+(value,)+devargs))
pywintypes.com_错误:(-2147352567,‘发生异常’,(0,无,无,无,0,-2147467259),无)
我已经运行了makepy.py并生成了Excel COM常量,但即使我使用诸如ChartType=3之类的数字类型,我也会得到相同的错误。如果我将ChartType设置为4(这是一条直线),它就可以正常工作


我注意到,如果将ChartType xlColumn设置为xlRandomText,则会得到一个干净的AttributeError:xlRandomText。因此,python似乎可以使用xlColumn,但在使用xlLine以外的任何东西自动化excel时遇到了问题。我想知道这是否是python/Excel 2010版?顺便说一句,我正在使用python 2.7和Excel 2010,我不知道为什么,但是xlColumn没有作为可用的图表类型列出:

但是,这可能适用于您:

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

谢谢你。使用Type而不是ChartType成功了。有趣的是,它可以与xlLine一起工作。用xlColumn和xlBar测试你的答案,效果很好。有时候win32com就是这样一个神秘的盒子。