[GIS] Listing layers from .mxd file using ArcPy for ArcGIS Pro

arcgis-proarcpylayersmxd

I am looking for a way to access a .mxd file's layer names and data sources.

In ArcGIS Desktop, I could do:

def print_layer_info(mxd_path):
    print(mxd_path + ":")
    mxd = arcpy.mapping.MapDocument(mxd_path)

    for df in arcpy.mapping.ListDataFrames(mxd, "*"):
        lyr = arcpy.mapping.ListLayers(mxd, "", df)[0]
        if lyr.supports("dataSource"):
            print "\t" + lyr.name
            print "\t\t" + lyr.dataSource
    del mxd

According to http://pro.arcgis.com/en/pro-app/arcpy/mapping/migratingfrom10xarcpymapping.htm, I could do:

p = arcpy.mp.ArcGISProject("file.mxd")

But that doesn't work. When I do that I get a OSError.

Since the arcpy.mapping.MapDocument is gone in ArcPy for ArcGIS Pro, what is the equivalent way of reading layer name and datasource of an .mxd file?

Best Answer

You can list the layers and the datasources of an MXD (ArcMap document) from ArcGIS Pro by simply importing the MXD into a project, and then following the same workflow you would do from ArcMap. (The syntax may vary a little).

  • Load up a project (like current, as the following code is run from the Python window)
  • Load the MXD into it
  • Find the map
  • List the layers

code:

p = arcpy.mp.ArcGISProject("CURRENT")
p.importDocument(r"C:\data\data.mxd")
for m in p.listMaps(): print(m.name)
> Layers
mxd = p.listMaps("Layers")[0]
for l in mxd.listLayers():
    if l.supports("dataSource"):
        print("{}, {}".format(l.name, l.dataSource))
> World Ocean Reference, 
> foo1, source1
> foo2, source2
> World Ocean Base, 
Related Question