Skip to content

Instantly share code, notes, and snippets.

@TheBryanMac
Created October 30, 2017 00:21
Show Gist options
  • Save TheBryanMac/8d944f49337580866629518338461b99 to your computer and use it in GitHub Desktop.
Save TheBryanMac/8d944f49337580866629518338461b99 to your computer and use it in GitHub Desktop.
Iterate through an ArcMap MXD, and export all layers to LYR files.
#Name: ArcMap Layers to LYR Files with Python
#Author: Bryan McIntosh
#Description: Iterate through an ArcMap MXD, and export all layers to LYR files.
import arcpy, os
#Set the working directory to where the Python file is stored
cwd = os.getcwd()
#Set the location of the MXD file
mxdPath = os.path.join(cwd,"MyMap.mxd")
#Set the location to store LYR files
layersOutPath = os.path.join(cwd,"export")
#Get the MXD and layers
mxd = arcpy.mapping.MapDocument(mxdPath)
layers = arcpy.mapping.ListLayers(mxd)
for layer in layers:
if str(layer.name) == str(layer.longName):
tempOutName = str(layer.name).replace("/","-")
tempOutName = str(tempOutName).replace(":","-")
fn = os.path.join(layersOutPath, str(tempOutName) + ".lyr")
layer.saveACopy(fn)
print "LYR Extraction Complete"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment