[英]How do I add data to an Excel chart using Python and win32com.client
I have been looking at here: https://msdn.microsoft.com/en-us/vba/excel-vba/articles/object-model-excel-vba-reference 我一直在这里看: https : //msdn.microsoft.com/en-us/vba/excel-vba/articles/object-model-excel-vba-reference
I created a chart in Excel manually and recorded the macro. 我在Excel中手动创建了图表并记录了宏。 Using that I am trying to navigate which methods to use. 使用这种方法,我试图浏览要使用的方法。 In my code below, ch is an embedded chart object in the worksheet. 在下面的代码中,ch是工作表中的嵌入式图表对象。 According to the above link, it should have a method called SetSourceData which takes Range as a required parameter. 根据上面的链接,它应该有一个名为SetSourceData的方法,该方法将Range作为必需参数。
import win32com.client as wc
xl = wc.Dispatch("Excel.Application")
xl.Visible = True
wb = xl.Workbooks.Add()
ws = wb.Worksheets.Add()
ws.Name = 'New Sheet'
ws.Cells(5,5).Value = 1
ws.Cells(6,5).Value = 1
ws.Cells(5,6).Value = 1
ws.Cells(6,6).Value = 1
ch = ws.ChartObjects().Add(100, 100, 100, 100)
#everything above works fine. problem is I can't add data to my chart.
ch.SetDataSource("E5:F5")
When I run the above, I get the following error: 当我运行以上命令时,出现以下错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-47-5b5d1a6d3109> in <module>()
30 ws.Cells(6,6).Value = 1
31
---> 32 ch.SetDataSource("E5:F5")
33
~\AppData\Local\Continuum\anaconda3\lib\site-
packages\win32com\client\dynamic.py in __getattr__(self, attr)
525
526 # no where else to look.
--> 527 raise AttributeError("%s.%s" % (self._username_, attr))
528
529 def __setattr__(self, attr, value):
AttributeError: Add.SetDataSource
Is there a way to show which methods are available when I import win32com.client? 导入win32com.client时,有什么方法可以显示哪些方法可用吗? Right now, I have to keep going to the MS website and dig through each method for chart creating/formatting, which is not efficient, but instructive I must admit. 现在,我必须继续访问MS网站,并仔细研究图表创建/格式化的每种方法,这种方法虽然效率不高,但我必须承认这一点。
ch.SetDataSource(Source=ws.Range('E5:F5'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.