Python - pywin32,对象没有属性'AddChart'

时间:2013-10-10 20:13:29

标签: python python-2.7 pywin32

我正在尝试在Excel工作表中嵌入图表,我能够找到有关使用AddChart函数的stackoverflow的信息,但是我收到此错误:

AttributeError: '<win32com.gen_py.Microsoft Excel 10.0 Object Library._Worksheet instance at 0x25233328>' object has no attribute 'AddChart'

有没有办法解决这个问题?

编辑:这是我的代码

xlLeft, xlRight, xlCenter = -4131, -4152, -4108 

import win32com.client
from win32com.client import constants as c

xl = win32com.client.gencache.EnsureDispatch("Excel.Application")
xl.Visible = True

Workbook = xl.Workbooks.Add()
Sheets = Workbook.Sheets

print "Debut"

Sheets(1).Range("A1:D1").Merge()
Sheets(1).Range("A1").Value = "Test"
Sheets(1).Range("A1").HorizontalAlignment = xlCenter

tableSize = 4

for y in xrange(1,tableSize):
    for x in xrange(0,tableSize):
            Sheets(1).Cells(y+1, x+1).Value = y*x

test = Sheets(1).Rows("1:1")
test.Interior.ColorIndex = 15

Sheets(1).Range("A2:D4").Select()

ch = xl.Sheets(1).AddChart()
ch.SetSourceData(Sheets(1).Range("A2:D4"))
ch.ChartType = c.xlLine

print "Fin"

1 个答案:

答案 0 :(得分:0)

AddChartmember of the Shapes collection。所以:

xl.Sheets(1).Shapes.AddChart();