我想使用Python和COM操作Excel文件中的数据和公式。我已经查看了几个Python模块,但还没有找到支持读取公式的功能。因此,我不得不使用COM。我不确定该如何称呼它,但是否有类似于Python、COM可用于Excel的所有类和方法的文档?这里有一些示例代码,但我似乎找不到任何详尽的文档来完成我想要做的事情。
import win32com
import win32com.client
xl = win32com.client.Dispatch("Excel.application")
xl.__doc__
我认为最后一行会打印出Excel应用程序的方法列表或文档。
sel = xl.Selection
当我按下xl后按TAB键时,上面的Selection()方法没有显示出来。我只知道有一个Selection方法,因为我在pyxll示例模块中读到了它。所以我认为还有更多的方法没有包含在TAB菜单中。
sel.Value = "Hello!"
sel.font.bold = true
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
sel.font.bold = true
NameError: name 'true' is not defined
sel.font.bold = 1
sel.font = "Constantia"
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
sel.font = "Constantia"
File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 570, in __setattr__
raise AttributeError("Property '%s.%s' can not be set." % (self._username_, attr))
AttributeError: Property '<unknown>.font' can not be set.
我猜测这个问题的答案,但我想学习正式的文档以了解可用的对象和方法。 有人可以指引我正确的方向吗? 先谢谢了!