List Layout Elements

435
1
06-04-2012 09:30 AM
RickPhelan
New Contributor
I am new to python and arcpy.
Information about set up:
A layer called "Horizontal BH" with an attribute called "BOEInterna" exist in the MXD
A text element called "Text" exist in the MXD

I am trying to print table attributes from a layer in my mxd to a text element, here is my code:

import arcpy
mxd=arcpy.mapping.MapDocument("T:/phelanr/PayDay.mxd")
mapLyr=arcpy.mapping.ListLayers(mxd, "Horizontal BH")[0]
welElem=arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "Text")[0]
rows=arcpy.SearchCursor(mapLyr.dataSource)
row=rows.next()
welElem.text=row.getValue("BOEInterna")

When I run the code I get the following error:
Runtime error <type 'exceptions.IndexError'>: list index out of range

See the attached image to see how I have set up the text element.

Also I want to make sure this will print every record in the attribute table for the attribute being focused on into the text element.

Thanks for any help.
Tags (2)
0 Kudos
1 Reply
MarcinGasior
Occasional Contributor III
Your error suggests that script couldn't find Layer or Layout Element with specified name.
So double check your names. (I suppose that text element in your screenshot has space before "Text")

If you want to put attribute value of every record into text element, it's convenient to build insert text variable first and then assign to text element:
        rows=arcpy.SearchCursor(mapLyr.dataSource)
        insertText = ""
        for row in rows:
            enterText += (row.getValue("BOEInterna") + " ") #you can use + "\n" to place every attribute value in a new line
        welElem.text=insertText
        mxd.save()
0 Kudos