Solved! Go to Solution.
import arcpy mxd = arcpy.mapping.MapDocument(r"C:\Temp\Project.mxd") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] count = 0 for lyr in arcpy.mapping.ListLayers(mxd, "", df): if count < 3: count +=1 name = lyr.name arcpy.FeatureClassToFeatureClass_conversion(lyr, r"C:\Temp", name + ".shp") print "Finished Exporting" del mxd, df, count, name
import arcpy mxd = arcpy.mapping.MapDocument(r"C:\Temp\Project.mxd") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] count = 0 for lyr in arcpy.mapping.ListLayers(mxd, "", df): if count < 3: count +=1 name = lyr.name arcpy.FeatureClassToFeatureClass_conversion(lyr, r"C:\Temp", name + ".shp") print "Finished Exporting" del mxd, df, count, name
Thanks for this script Jonathan.
I am trying to run it with over 1,000 layers in the TOC. I simply removed the lines regarding the Count but am encountering an error that a specific object already exists. I know it is relatively simple to override this but I am not sure how.
I have 182 GDBs loaded into the TOC, all with the 7 same feature classes that I need exported in this fashion.
I would greatly appreciate your help.
what would a python script look like if all of the layers that needed to be exported were rasters? Would it be possible to repost the script so that it reads vertically, on multiple lines? Also, I am confused part of the code: what does the "count=0", "count +=1" and the "name = lyr.name" lines mean?
import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Temp\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
count = 0
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
if count < 3:
count +=1
name = lyr.name
arcpy.FeatureClassToFeatureClass_conversion(lyr, r"C:\Temp", name + ".shp")
print "Finished Exporting"
del mxd, df, count, name
Not sure why the original comment came out in a single line, but here it is posted correctly. First, you'd still list the layers in the data frame on line 5. You could find which are rasters by following the Layer documentation. The name = lyr.name is from that documentation as well, basically just getting the name of the layer from the TOC. The original question was how to get the first x layers, so in the example, a variable is set to 0 to act as a counter, then the loop starts through all layers, adding 1 to the counter as it loops through each layer. Once the number of layers gone through is less than 3, continue on to export the feature class. To process rasters, switch the arcpy.FeatureClassToFeatureClass toop with the arcpy.CopyRaster tool or whatever other tool you want to use:
import arcpy, os
mxd = arcpy.mapping.MapDocument(r"C:\Temp\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
if lyr.isRaster():
name = lyr.name
outRaster = os.path.join(r"C:\Temp","{0}.tiff".format(name))
arcpy.CopyRaster_management(lyr, outRaster)
print "Finished Exporting"
So line 4 loops through the layers, line 5 checks if it's of type Raster layer, line 6 gets the name of the layer from the TOC, line 7 uses the os.path.join function to construct the path to the output raster using the format function to create the output name, and line 8 runs the tool.
thank you! your explanation of the original script, as well as the modification of the script to work with rasters are both very helpful. I have run the script on polylines, polygons, and point layers with success.