I have a piece of code that selects a layer based on the data frame extent. the code works when I use the code within ArcMap. However, as soon as I try run this as a stand alone script, nothing gets selected. So some empty Excel files get created. I have no understanding of why running this as a standalone script would be any different.

import os
import arcpy
import sys

#create polygon for DF extent
def getDataFramePoly(mxd):
    mxd = mxd
    df = arcpy.mapping.ListDataFrames(mxd)[0]
    extent = df.extent
    array = arcpy.Array()
    polygon = arcpy.Polygon(array)
    return polygon

#get relevant layers function
def getLayers(layers, datasources):
    analysis_layers = []
    layers = layers
    for lyr in layers:
        if lyr.supports('DATASOURCE') and str(lyr.dataSource)in datasources:
    return analysis_layers

mxd = arcpy.mapping.MapDocument(r"G:\mxdName.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0] #First Dataframe
extent = df.extent

#select layer by DF
df_extent = "DF_EXTENT"
polygon = getDataFramePoly(mxd)

#feature classes that should be included in analysis
dataSources = [r'datasourcePath' r'datapayt']

analysis_layers = getLayers(arcpy.mapping.ListLayers(mxd,"",df), dataSources)
excel_out_path = r'C:\Users\Bbrokling\Desktop\Test'

#Select visible features
i = 0
for lyr in analysis_layers:
    desc = arcpy.Describe(lyr)
    if not desc.FIDSet  == '':
        print 'Layer has a selection' 
    if lyr.symbologyType == u"UNIQUE_VALUES":
        desc = arcpy.Describe(lyr)
        symField = lyr.symbology.valueField
        symClassValues = lyr.symbology.classValues
        otherValues = lyr.symbology.showOtherValues
        if not otherValues:
            for x in desc.fields:
                if x.name == symField:
                    print x.type
                    fieldType = x.type
            if fieldType in ['SmallInteger', 'Double', 'Integer']:
                newclassvalues = tuple([float(x) for x in symClassValues])
                newclassvalues = tuple([x.encode('UTF8') for x in symClassValues])
            newQuery = "{} in {}".format(symField, newclassvalues)
    out = r'%s\%s.xls' % (excel_out_path,lyr.name)
    desc = arcpy.Describe(lyr)
    if not desc.FIDSet  == '':
            print 'Layer has a selection'
    table_view_Name =  "tableView%s" % str(i)
    arcpy.MakeTableView_management(lyr, table_view_Name)
    arcpy.TableToExcel_conversion(table_view_Name, out)
    i += 1
  • You need to indentent properly. Also what is "analysis_layers", how do you create it?
    – BERA
    Oct 16, 2017 at 9:07
  • Analysis_Layers is a list of layers created using arcpy.mapping.ListLayers(mxd,"",df) Oct 16, 2017 at 9:12
  • Your newQuery variable hasn't got the enclosed bracket at the end.
    – MacroZED
    Oct 16, 2017 at 10:01
  • Have you had a look at df_extent polygon? For example by copy features?
    – BERA
    Oct 16, 2017 at 10:21

1 Answer 1


I suspect that you have not created feature layers for your datasets. I assume that analysis_layers are merely a list of the datasets. These need to be converted into feature layers before continuing on with your script. Therefore use the following in your script:

for layer in analysis_layers:
    lyr_name = arcpy.Describe(layer).name
    lyr = arcpy.MakeFeatureLayer_management(layer, lyr_name)
  • Ok I tested rather all the items in the analysis_layers list were feature_layers Oct 16, 2017 at 9:50
  • You should update your question to include your whole script. If you haven't explicitly used MakeFeatureLayer in your stand alone script, then they wont be feature layers. Make sure that you are not testing this within the ArcMap python window as it will treat all layers as feature layers.
    – MacroZED
    Oct 16, 2017 at 9:55
  • I tested the layers in idle looping through each layer in the analysis_layers and testing by using lyr.isFeatureLayer Oct 16, 2017 at 10:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.