Hi there,
I am working on a Multi-scale layer map using ArcMap.
Since I have several hundred of different map layer in my *.mxd I'd like to save for each individual map layer a Layer File.
I found a script which already works but I'd like to get it to save the Layer Files for each Group Layer in a separate folder.
So all Layer Files should end up in a separate folder like the Group Layer is called in my *.mxd.
I am not a coder and was already happy with the script I found but it would be great if somebody can help me with extending this script.
This is the script I was using:
import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd😞
lyr.saveACopy(lyr.name + ".lyr")
Thank you for your help.
Pierre
Solved! Go to Solution.
If you are doing this from the ArcMap Python window the following code should work:
import os
basepath = r'G:\xTemp\MapLayers'
mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.isGroupLayer == True:
grpPath = os.path.join(basepath, str(lyr))
if not os.path.exists(grpPath):
os.makedirs(grpPath)
else:
fn = os.path.join(basepath, str(lyr) + ".lyr")
lyr.saveACopy(fn)
print "Saved: " + fn
Just change the basepath to the output directory.
I think you have to go back to layer properties within arcpy itself. There are some code samples there that shows how to save layers with different names and saving locations. You can modify a workflow based upon these values.
If you are doing this from the ArcMap Python window the following code should work:
import os
basepath = r'G:\xTemp\MapLayers'
mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.isGroupLayer == True:
grpPath = os.path.join(basepath, str(lyr))
if not os.path.exists(grpPath):
os.makedirs(grpPath)
else:
fn = os.path.join(basepath, str(lyr) + ".lyr")
lyr.saveACopy(fn)
print "Saved: " + fn
Just change the basepath to the output directory.
Thank you I appreciate your help.
I tried this from the ArcMap Python window:
import os
basepath = r'N:\Temp\layerstyle'
mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.isGroupLayer == True:
grpPath = os.path.join(basepath, str(lyr))
if not os.path.exists(grpPath):
os.makedirs(grpPath)
else:
fn = os.path.join(basepath, str(lyr) + ".lyr")
lyr.saveACopy(fn)
print "Saved: " + fn
Unfortunately it came back with an error message saying:
Parsing error SyntaxError : invalid syntax (line 6)
I am not a Python coder so any help would be great.
Thank you.
from the else statement onward it is indented too much, see the source example provided
The script is working now. Thank you very much it saved me a lot of time.
Whoops! Replied at the same time again!
Pierre, try replacing line six with grpPath = os.path.join(basepath, lyr)
and line ten with
fn = os.path.join(basepath, lyr + ".lyr")
Hey pierre, can you mark this question as answered? This will help other people with the same problem find the answer. thanks!