Select to view content in your preferred language

Save Layer File with Python

10049
8
Jump to solution
04-01-2015 07:07 PM
PierreKurth
Emerging Contributor

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

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
OwenEarley
Frequent Contributor

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.

View solution in original post

8 Replies
DanPatterson_Retired
MVP Emeritus

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.

0 Kudos
OwenEarley
Frequent Contributor

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.

PierreKurth
Emerging Contributor

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.

0 Kudos
DanPatterson_Retired
MVP Emeritus

​from the else statement onward it is indented too much, see the source example provided

PierreKurth
Emerging Contributor

The script is working now. Thank you very much it saved me a lot of time.

0 Kudos
SepheFox
Deactivated User

Whoops! Replied at the same time again!

0 Kudos
SepheFox
Deactivated User

Pierre, try replacing line six with          grpPath = os.path.join(basepath, lyr)

​and line ten with

fn = os.path.join(basepath, lyr + ".lyr")

0 Kudos
SepheFox
Deactivated User

Hey pierre, can you mark this question as answered? This will help other people with the same problem find the answer. thanks!

0 Kudos