1

I recently found this layer properties script that summarizes layer properties in a single file or a directory and all subdirectories: http://www.esri.com/esri-news/arcuser/winter-2015/create-a-python-tool-that-summarizes-arcmap-layer-properties. It would be enormously useful to me if it worked. It does, in one case: running it on the MXD (not the directory) included with the downloaded sample file exactly as download.

The first problem is the tool doesn’t find layers within groups. Many of my maps have groups or even a group under which I have all my layers. When modifying the layout on a map with lots of data, turning off all the layers speeds time to make changes to the layout; adding a top-level group under which I have all layers means all I have to do it turn off the group to turn off all the layers. Also, I find grouping a good way to organize layers and a great way to develop several maps within a single MXD. To test my hypothesis, I put some layers in the downloaded file within a group, and those layers did not appear in the generated CSV.

The second problem is the tool doesn’t work on directories. The error I get is on this line:

for filename in filenames[2]:

And the message is:

NameError: name ‘filenames’ is not defined
  • Looks like the script may be off, try: for dirpath, dirnames, filenames in os.walk(mxddirectory): – Aaron May 8 '15 at 17:00
  • You could also contact the author directly (his email is at the bottom). Maybe you could encourage him to post his tool in complete form in the ArcGIS galleries too. Would be easier long term for folks to provide feedback. – DWynne May 8 '15 at 17:34
  • For this to be on-topic for our focussed Q&A format I think we will need you to extract a snippet from it. Otherwise, it seems like a code debugging/writing question except the code itself is missing. – PolyGeo May 9 '15 at 6:11
  • @ChrisW - or at least split the relevant parts across the question and an answer. bkepl - if you have not already taken the Tour it is well worth doing so. You will see how we look for one question per question - yours seems to have two - just something to think about for next time. – PolyGeo May 10 '15 at 23:49
  • Sorry, I'm new here and getting used to protocol. My intent was not to reopen the question but to offer what I learned in the hope that others would benefit from it. I'll do as you suggest and take the tour. – bkepl May 11 '15 at 1:29
3

For the second problem, I contacted the author (as suggested by DWynne) who gave the correction:

for filename in dirpath[2]:

That corrected the problem. The author told me he contacted Esri shortly after this made it into print but Esri hasn't changed it on the web. He told me he'd contact them again.

For the first problem of this script not listing layers in a group layer, this ought to fix that. Where the code says:

for layer in arcpy.mapping.ListLayers(mxd):

I added after it (sorry, I know the indenting isn't correct)

if lyr.supports("DATASOURCE"):

I found this on an Esri Help page. I'll try it out when I'm back at work. I hope this helps others who have run across this issue.


I have tested this with the problem of the script not listing layers within a group. I also expanded it to allow the output CSV to be automatically named based on the file or folder entered. Here is my code; I hope someone can make use of it.

#Import modules...
import arcpy, os, fnmatch, csv

#User input variables...
mxddirectory = arcpy.GetParameterAsText(0)
mxd_single = arcpy.GetParameterAsText(1)
outputcsvlocation = arcpy.GetParameterAsText(2)

#Create an empty list of ArcMap documents to process...
mxd_list=[]

#If an output location for the CSV is specified, use it...
if len(outputcsvlocation) > 0:
  outputcsvlocation_2 = outputcsvlocation
#...otherwise if a singe MXD is specified, name the CSV for it, or...
elif len(mxd_single) > 0:
  outputcsvlocation_2 = mxd_single.replace(".mxd",".csv")
#...if a folder location is specified, save a CSV called ListLayers.csv in that folder.
else:
  outputcsvlocation_2 = mxddirectory + "\ListLayers.csv"

#If a user defined a single mxd, add its path to the list...
if len(mxd_single) > 0:
  mxd_list.append(mxd_single)
#Otherwise walk through the input directory, adding paths for each .mxd file found to the list...
else:
  for dirpath in os.walk(mxddirectory):
    for filename in dirpath[2]:
      if fnmatch.fnmatch(filename, "*.mxd"):
        mxd_list.append(os.path.join(dirpath[0], filename))

#Iterate the list of mxd paths and gather property values then write to csv file...
if len(mxd_list) > 0:
  #Create the csv file...
  outputcsv = open(outputcsvlocation_2,"wb")
  writer = csv.writer(outputcsv, dialect = 'excel')
  #Write a header row to the csv file...
  writer.writerow(["mxdpath", "layername", "layerdescription", "layersource"])
  #Iterate through the list of ArcMap Documents...
  for mxdpath in mxd_list:
    mxdname = os.path.split(mxdpath)[1]
    try:
      mxd = arcpy.mapping.MapDocument(mxdpath)
      #Iterate through the ArcMap Document layers...
      for layer in arcpy.mapping.ListLayers(mxd):
        if layer.supports("DATASOURCE"): #List all layers that have data, skipping group layers.
          layerattributes = [mxdpath, layer.longName, layer.description, layer.dataSource]
          #Write the attributes to the csv file...
          writer.writerow(layerattributes)
    except:
      arcpy.AddMessage("EXCEPTION: {0}".format(mxdpath))
    del mxd
  #close the csv file to save it...
  outputcsv.close()
#If no ArcMap Documents are in the list, then notify via an error message...
else:
  arcpy.AddError("No ArcMap Documents found. Please check your input variables.")

Thanks to all, especially the original code author.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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