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?
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.
Sign up to join this communityIn 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?
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()
You can try this, it requires saving your layers out to a lyr file on disk before adding it to the second map.
In map one, run this code:
arcpy.SaveToLayerFile_management("yourlayerinmap", r'youroutputlocation')
In map two, run this code:
mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd)[0]
layer = arcpy.mapping.Layer('youroutputlocation')
arcpy.mapping.AddLayer(df, layer)