How do I import a COM object namespace/enumeration in Python?

前端 未结 4 1174
无人及你
无人及你 2020-12-29 14:07

I\'m relatively new to programming/python, so I\'d appreciate any help I can get. I want to save an excel file as a specific format using Excel through COM. Here is the c

4条回答
  •  情歌与酒
    2020-12-29 15:10

    This question is a bit stale, but for those reaching this page from Google (as I did) my solution was accessing the constants via the win32com.client.constants object instead of on the application object itself as suggested by Eric. This lets you use enum constants just like in the VBE:

    >>> import win32com.client
    >>> xl = win32com.client.gencache.EnsureDispatch('Excel.Application')
    >>> C = win32com.client.constants
    >>> C.xlWorkbookNormal
    -4143
    >>> C.xlCSV
    6
    >>> C.xlErrValue
    2015
    >>> C.xlThemeColorAccent1
    5
    

    Also, unless you've manually run the makepy utility, the constants may not be available if initializing the application with the regular win32com.client.Dispatch(..) method, which was another issue I was having. Using win32com.client.gencache.EnsureDispatch(..) (as the questioner does) checks for and generates the Python bindings at runtime if required.

    I found this ActiveState page to be helpful.

提交回复
热议问题