[GIS] How to copy layers between MXD’s with arcpy

arcpygroup-layertable of contents

In ArcGIS I need to being able to have a couple of MXD documents open and copy-paste groups of layers from one MXD's table of contents to another with python script.

Is this possible and, if so, how?

Best Answer

This code snippet will copy all the group layers from a template mxd and put them in the currently opened MXD in ArcMap, then save the mxd

#assuming code being run in the python interpreter window in ArcMap, else give it a path to the mxd
mxd = arcpy.mapping.MapDocument("Current")
df = mxd.activeDataFrame

#mxd to copy the group layers from... give it the path to your mxd that has the layers
layerTemplateMXD = arcpy.mapping.MapDocument(r"path to mxd that has layers")
templatedf = layerTemplateMXD.activeDataFrame

for layer in templatedf:
    if layer.isGroupLayer:
        # add the group layer to the bottom to maintain the order from the template mxd
        arcpy.mapping.AddLayer(df,layer,"BOTTOM")

# save it now that you've added the other layers
mxd.save()
Related Question