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!
Solved! Go to Solution.
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)
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)
Perfect! Exactly what I was looking for. Thanks!
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.