简体   繁体   English

如何使用Python和win32com.client将数据添加到Excel图表中

[英]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.

相关问题 如何使用 win32com.client 通过 python 将 XLA 添加到 excel 中? - How do I get an XLA add in into excel through python using win32com.client? 如何使用 python win32com.client 在 Excel 中添加折线? - How to Add polyline in Excel with python win32com.client? 如何关闭另一个应用程序打开的 Excel 文件? 将 python 与 win32com.client 一起使用 - How to close an Excel file opened by another application? Using python with win32com.client 如何选择win32com.client必须在python中使用的excel版本? - How to choose the version of excel which win32com.client has to use in python? 如何通过使用“ win32com.client as win32”来知道excel使用的行数和列数 - how to know used row count and column count of excel by using “win32com.client as win32” win32com.client Python - Excel 任务在告诉应用程序退出后仍然运行 - win32com.client Python - Excel task remains running after telling application to quit 用于Select-Case(excel)的Python Win32com.Client模块中的语法是什么 - What is the syntax in Python Win32com.Client module for Select-Case (excel) 使用win32com.client计算Excel文件中的页数 - Count number of sheets in an Excel file using win32com.client 使用win32com.client打开excel窗口后需要最大化 - Need to maximize the excel window after opening it using win32com.client Python - 使用 win32com.client 将 Excell 单元格范围格式化为表格 - Python - Using win32com.client to format an Excell cell range as table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM