Layer Description to Text Tile

1451
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
JeffBarrette
Esri Regular Contributor
Is it possible that your layer description is a number?  Could you try changing line 39 to something like:

outFile.write("\t\t\t Description: " + str(lyr.description) + "\n")

Jeff
0 Kudos
MollyWatson
New Contributor III
Jeff, thanks for the suggestion. I copied your suggestion for line 39 and got a new error:
UnicodeEncodeError: 'ascii' coded can't encode character u'\u2019' in position 83: ordinal not in range(128)
0 Kudos
JeffBarrette
Esri Regular Contributor
Can you tell me what the value of your description is?

print lyr.description

Jeff
0 Kudos