Layer Description to Text Tile

1418
12
10-05-2011 10:16 AM
MollyWatson
New Contributor III
Hello,

I am new to Python and have been researching the forums and other websites for tips on writing a Python code to get the layer descriptions from layers in an MXD and output those descriptions in a text file. This is what I have so far, but I am getting an exceptions.AttributeError 'str' object has no attribute '_arc_object'. I'm not sure what that error means or how to correct it.  Thanks for any help!

# Setup
# Import arcpy module
import sys, string, os, arcpy
from arcpy import env
from arcpy import mapping

# Data: Declare environment variables
env.overwriteOutput = 1
env.workspace = workspace = r"V:\gislu\_PlanX\Layer"
output = r"V:\gislu\_PlanX\Temp.txt"

#Create Output Text File
outFile = open (output , "w")
   
#Reference MXD and Data Frame
mxd = arcpy.mapping.MapDocument("Current")
df = arcpy.mapping.ListDataFrames ("Layers") [0]

# Reference each layer in data frame
lyrList = arcpy.mapping.ListLayers(mxd, "", df)
for lyr in lyrList:
    outFile.write("\n")
    outFile.write("\t\t LAYER: " + lyr.name + "\n")
if lyr.supports("DESCRIPTION"):
    outFile.write("\t\t\t Description: " + lyr.description + "\n")
else:
    outFile.write("t\t\t Description: N/A \ n")
Tags (2)
0 Kudos
12 Replies
JakeSkinner
Esri Esteemed Contributor
Looks like the problem is with your 'df' variable.   You will need to reference the MXD and then the data frame.  Change it to the following and I believe everything should work:

df = arcpy.mapping.ListDataFrames (mxd, "Layers") [0]
0 Kudos
AlessandroCinnirella
New Contributor III
#Reference MXD and Data Frame
mxd = arcpy.mapping.MapDocument("Current")
the "CURRENT" keyword works only if you run the script from within the pythonwindow
if you run your script from outside arcgis you must put the path/to/mxd
and, when declaring the df:

not
df = arcpy.mapping.ListDataFrames ("Layers") [0]
but
df = arcpy.mapping.ListDataFrames (mxd) [0]

ciao,
AC
0 Kudos
MollyWatson
New Contributor III
Thank you. Your suggestions made the script run successfully. However, the output text file only contains the layer names and not the descriptions.  I've attached it so you can see. Most of the layers have layer descriptions so they should show up.
0 Kudos
RyanKelley
New Contributor II
I couldn't replicate this issue... are you sure you have layer descriptions (just checking)?

Obviously all of you layers support descriptions, or you'd be getting "Description N/A" somewhere...
0 Kudos
JeffBarrette
Esri Regular Contributor
Check out the following sample scripts:

http://resources.arcgis.com/gallery/file/geoprocessing/details?entryID=A910AB18-1422-2418-3418-3885D...

There are some reporting tools that do exactly what you want.

Jeff
0 Kudos
MollyWatson
New Contributor III
Yes all but 3 of layers have descriptions. When I ran it again this morning, one description showed up but no others. And for the 3 layers with no descriptions, there was no N/A after it.  Did the script work with your data when you tried to replicate the error?
0 Kudos
MollyWatson
New Contributor III
I wonder if it is an issue with group layers. The one layer description that did show up was not a group layer.  Most of the other layers in the MXD are group layers and the descriptions are not showing up. When you ran the script, were your layers group layers or single layer files?
0 Kudos
MollyWatson
New Contributor III
I changed the script to try to account for the group layers but it only displays the description for the layer at the very bottom of the group layer:

# Setup
# Import arcpy module
import sys, string, os, arcpy
from arcpy import env
from arcpy import mapping

# Data: Declare environment variables
env.overwriteOutput = 1
env.workspace = workspace = r"V:\gislu\_PlanX\Python"
output = r"V:\gislu\_PlanX\Python\Temp.txt"

#Create Output Text File
outFile = open (output , "w")
   
#Reference MXD and Data Frame
mxd = arcpy.mapping.MapDocument(r"V:\gislu\_PlanX\Python\PlanXLayers.mxd")
df = arcpy.mapping.ListDataFrames (mxd, "Layers") [0]

# Reference each layer in data frame
lyrList = arcpy.mapping.ListLayers(mxd, "", df)
for lyr in lyrList:
    outFile.write("\n")
    outFile.write("\t\t LAYER: " + lyr.longName + "\n")
if lyr.isGroupLayer:
    outFile.write("\t\t\t Description: " + lyr.description + "\n")
else:
    outFile.write("t\t\t Description: N/A")
0 Kudos
MollyWatson
New Contributor III
Thanks the layer reporting script helped. I basically copied and pasted the items I want the report to show but I get an error on line 39 "UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 100: ordinal not in range(128) 'ascii' codec can't encode character u'\u2019' in position 100: ordinal not in range(128)." Do you know what this means?  My new script is attached.
0 Kudos