Select to view content in your preferred language

Save all the layers in table of contents to layer files

956
4
08-15-2012 09:26 PM
RaziMosadeghi
Deactivated User
Hi, I have this big mxd containing 400+ layers under different groups. I was wondering if there is any python scripting to save all the layers to the layer files.

Regards
Razi
Tags (2)
0 Kudos
4 Replies
ChristopherThompson
Frequent Contributor
This should work for you.. replace the stuff in red with the appropriate file paths. The script will find all the dataframes in your mxd and then save the layer files to the folder you specify.

  • Group layers are saved as such and named with the word 'Group' at the front so they are easily identified as such, but the individual layers inside a group are also saved - that may not be behavior you want so you may need to tweak that.


  • The naming of the saved layers is drawn directly from how the layers are named in your table of contents; again, if that isn't what you want you'll need to sort out how to name them more appropriately.

import arcpy, arcpy.mapping
ws = r'YOUR FILE PATH HERE TO WHERE LAYERS GET SAVED'
mxd = r'FILE PATH TO YOUR MAP DOCUMENT HERE'
mapdoc = arcpy.mapping.MapDocument(mxd)
dframes = arcpy.mapping.ListDataFrames(mapdoc)
for dframe in dframes:
    lyrs = arcpy.mapping.ListLayers(mapdoc,'',dframe)
    for lyr in lyrs:
        if lyr.isGroupLayer:
            l_name = 'group_' + lyr.name
        else:
            l_name = lyr.name
        arcpy.SaveToLayerFile_management(lyr,ws + '\\' + l_name + '.lyr')
0 Kudos
RaziMosadeghi
Deactivated User
Thank you for the quick reply
I tried the script but nothing happens! I have also tried the following script, which didn't work!

import arcpy
ws = arcpy.env.workspace(r"M:\SIS\ArcGIS Workspace\GIS_Project_lyrs\Dekho\lyrs\Test")
mxd = arcpy.mapping.MapDocument("CURRENT")
dframes = arcpy.mapping.ListDataFrames(mxd)
for dframe in dframes:
   lyrs = arcpy.mapping.ListLayers(mxd,'',dframe)
for lyr in lyrs:
   if lyr.isGroupLayer:
       l_name = 'group_' + lyr.name
   else:
       l_name = lyr.name
   arcpy.SaveToLayerFile_management(lyr,ws + '\\' + l_name + '.lyr')

The mxd only has one datafram and I certainly need grouped layers as well as individual layers.
Would appreciate your help.
Thanks
0 Kudos
ChristopherThompson
Frequent Contributor
thats odd, because i tested that script against several mxds before i posted it and it worked like a charm.  How are you executing the script?  I noticed this line inside the code you just posted:
mxd = arcpy.mapping.MapDocument("CURRENT")

the "CURRENT" keyword only works if you are running the script from within ArcMap I believe.  To run the code I gave you, open it with IDLE, replace the stuff in red, save it, then from the Run menu, select 'Run Module' and see if that helps.

Also, this line won't work I don't think:
ws = arcpy.env.workspace(r"M:\SIS\ArcGIS Workspace\GIS_Project_lyrs\Dekho\lyrs\Test")

you can't set an object/variable like ws with that statement it has to be on a line of its own.. you can do:
arcpy.env.workspace(r"M:\SIS\ArcGIS Workspace\GIS_Project_lyrs\Dekho\lyrs\Test")

or
ws = r"M:\SIS\ArcGIS Workspace\GIS_Project_lyrs\Dekho\lyrs\Test"
arcpy.env.workspace(ws)

So you might try fixing those things and see if that works better for you.
0 Kudos
RaziMosadeghi
Deactivated User
Thank you that worked. I also got the following form ESRI Australia which worked perfectley

import arcpy
import os

mxdpath = arcpy.GetParameterAsText(0)
outdir = arcpy.GetParameterAsText(1)

mxd = arcpy.mapping.MapDocument(mxdpath)
arcpy.env.workspace = outdir

i = 0
for lyr in arcpy.mapping.ListLayers(mxd):
    outname = lyr.name
    if os.path.exists(outdir):
        outname = outdir + "\\" + outname
    i = 0
    out_i = outname + ".lyr"
    print out_i
    while os.path.exists(out_i) and i < 10:
        out_i = outname + "_" + str(i) + ".lyr"
        i += 1
    outname = out_i   
    lyr.saveACopy(outname)
0 Kudos