0

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?

2
  • If you already have the mxds open in ArcMap, it may work just as well to just drag and drop layers among the mxds. With Python, you don't even have to have them all open. Just a thought.
    – Baltok
    Oct 23, 2014 at 14:27
  • I deleted my previous "obvious" answer because no longer relevant with the inclusion of arcpy Oct 23, 2014 at 14:43

2 Answers 2

4

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()
3

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)
4
  • 1
    You could have both maps open at the same time as two different objects and avoid the intermediate layer files. Oct 23, 2014 at 16:02
  • Jason,How can i do it at the same time with arcpy?
    – newGIS
    Oct 26, 2014 at 5:03
  • Jason,i don't understand your answer
    – newGIS
    Jan 11, 2015 at 7:48
  • i mean in stand-alone script ?
    – newGIS
    Jun 1, 2016 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.