In Visual studio there is an option to register as an Interop COM. I want to achieve the same using python language. If someone can help me how to go about this. I did my research but couldnt find anything.
-
Did you find docs.activestate.com/activepython/2.7/pywin32/com_overview.htmlJanne Karila– Janne Karila2013-09-20 06:46:29 +00:00Commented Sep 20, 2013 at 6:46
-
Although it seems like not a good SO question, but people seeking the answer to this question will definitely being brought to this page. An up vote from me!swdev– swdev2014-03-31 06:26:42 +00:00Commented Mar 31, 2014 at 6:26
2 Answers
Like my namesake comments above. Use the pywin32 module. Alternatively you can use the comtypes module instead. Because python is a interpreted language you can use COM in 2 separate ways. By generating a interoperability layer or by dynamic typing. Both comtypes and pywin32, can do dynamic typing. Additionally pywin32's win32com module can also generate a interoperability layer, this has a drawback tough as dynamic typing is often easier to handle. Here is a simple example that generates a interop like compatibility layer (if on does not exist) in excel. Then the script opens a existing worksheet and edits the first cell value:
from win32com.client.gencache import EnsureDispatch
xl = EnsureDispatch ("Excel.Application")
xl.Visible = True
wb = xl.Workbooks.Open(r'c:\temp\test.xlsx')
sht = wb.Worksheets(1)
sht.Cells(1,1).Value = "yeah this works"
PS: Stack overflow already automatically suggests a lot of resources for you to follow in the related sidebar.