gpt4 book ai didi

python - 无法获取 Excel ChartObjects 计数

转载 作者:行者123 更新时间:2023-11-30 23:38:22 30 4
gpt4 key购买 nike

有谁知道如何获取 Excel ChartObjects 计数吗?在 VBA 示例中,我可以在一张纸中获取所有图表名称和图表计数。但我不知道为什么,python 无法获取图表计数。我尝试了所有其他 ChartObjects 属性,除了 Count 之外,所有属性都有效。

[MSDN Library][1]
ChartObjects.Application Property (Excel)
Not Working ===> ChartObjects.Count Property (Excel)
ChartObjects.Creator Property (Excel)
ChartObjects.Height Property (Excel)
ChartObjects.Left Property (Excel)
ChartObjects.Locked Property (Excel)
ChartObjects.Parent Property (Excel)
ChartObjects.Placement Property (Excel)
ChartObjects.PrintObject Property (Excel)
ChartObjects.ProtectChartObject Property (Excel)
ChartObjects.ShapeRange Property (Excel)
ChartObjects.Top Property (Excel)
ChartObjects.Visible Property (Excel)
ChartObjects.Width Property (Excel)

ex.
Python:
workbook.Sheets(2).ChartObjects.Count

VBA:
ActiveWorkbook.Sheets(lngS).ChartObjects.Count

Python 错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "F:\Python27\lib\site-packages\win32com\client\__init__.py", line 465, in
__getattr__
raise AttributeError("'%s' object has no attribute '%s'" % (repr(self), attr
))
AttributeError: '<win32com.gen_py.Microsoft Excel 14.0 Object Library.ChartObjec
t instance at 0x51078984>' object has no attribute 'Count'

[1]: http://msdn.microsoft.com/en-us/library/ff846604%28v=office.14%29.aspx

最佳答案

回答我的问题:ChartObjects 是一个函数。所以不能有 count 属性。下面的代码段修复了 Python 和 Visual Basic 中的问题。

注意,VBA 不需要此步骤。

Python or VB:
oWorksheet = workbook.Worksheets("Created.vs.Resolved")
oChart = oWorksheet.ChartObjects()
chart_count = oChart.Count

VBA:
ActiveWorkbook.Sheets(lngS).ChartObjects.Count

关于python - 无法获取 Excel ChartObjects 计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14621224/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com