Accessing ArcObjects from Python – Method and Steps

arcobjectscomtypespython

I would like to be able to script some things that are not exposed via arcgisscripting or ArcPy.

How do I access ArcObjects from Python?

Best Answer

Download and install comtypes*, put the Snippets module from Mark Cederholm in PYTHONPATH, and you're all set.

from snippets102 import GetLibPath, InitStandalone
from comtypes.client import GetModule, CreateObject
m = GetModule(GetLibPath() + "esriGeometry.olb")
InitStandalone()
p = CreateObject(m.Point, interface=m.IPoint)
p.PutCoords(2,3)
print p.X, p.Y

For background see Mark Cederholm's presentations for UberPyGeeks on "Using ArcObjects in Python". There are separate ones for VBA and C++ developer perspectives. They use Visual Studio (yes Express is ok) and the Windows SDK, but these aren't required, just ArcGIS, Python and comtypes are sufficient.

Getting the Snippets module

* Note for 10.1+ you need to make a small change to automation.py in the comtypes module. See ArcObjects + comtypes at 10.1.


Next steps

...or: brain gone snaky? Looking at the c# code examples makes your eyes swim, and try as you might you just can't think like a crane? Look here:

Related Question