Export Layers from Map Document to Shapefiles

2071
3
Jump to solution
08-29-2014 05:45 AM
mpboyle
Occasional Contributor III

I'm trying to export the contents of a map document to shapefiles and then zip the folder that contains the shapefiles.  Below is the script I have written so far.  It only exports 1 layer instead of the 3 layers that are in the map document.  I'm wondering how to iterate through the data sources and export each one?

import arcpy

import os

import glob

import shutil

import time

MapDoc = r'\\...path to mapdoc.mxd'

ShpDirectory = r'\\...path to ShapefileOut folder'

ShpZip = r'\\...path to ShapefileZip folder'

mxd = arcpy.mapping.MapDocument(MapDoc) 

lyrs = arcpy.mapping.ListLayers(mxd)

for lyr in lyrs: 

    try: 

        print(lyr.name,lyr.dataSource)

    except: 

        print('Unable to retrieve layer information')

inFeatures = lyr.dataSource

inFeatureName = lyr.name

arcpy.FeatureClassToShapefile_conversion(inFeatures, ShpDirectory)

shutil.make_archive(ShpZip, "zip", ShpDirectory)

del mxd

I'm wondering what needs to be done to export each individual layer's data source?

Thanks in advance!

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hi Matthew,

You will just need to indent a couple lines and your code should work:

for lyr in lyrs:   

    try:   

        print(lyr.name,lyr.dataSource) 

    except:   

        print('Unable to retrieve layer information') 

 

    inFeatures = lyr.dataSource 

    inFeatureName = lyr.name 

 

    arcpy.FeatureClassToShapefile_conversion(inFeatures, ShpDirectory)

View solution in original post

0 Kudos
3 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Matthew,

You will just need to indent a couple lines and your code should work:

for lyr in lyrs:   

    try:   

        print(lyr.name,lyr.dataSource) 

    except:   

        print('Unable to retrieve layer information') 

 

    inFeatures = lyr.dataSource 

    inFeatureName = lyr.name 

 

    arcpy.FeatureClassToShapefile_conversion(inFeatures, ShpDirectory)

0 Kudos
mpboyle
Occasional Contributor III

Jake Skinner

Perfect!  Exactly what I was looking for.  Thanks!

0 Kudos
FilipKrál
Occasional Contributor III

Hi,

I just want to point out the Consolidate Map tool‌ that exports the contents of a map document. There are some more Consolidate <Something> tools available.

It does not give you as fine control as you can achieve in Python if you wanted to exclude some layers for example, but it can be pretty useful still.

Cheers, F.