Select to view content in your preferred language

Unable to find TEXT_ELEMENTs with ListLayoutElements()

2188
5
12-15-2011 11:41 AM
CherylSharpe
New Contributor
I'm fairly experienced with Python, but am new to arcpy, and have moderate skill with ArcMap. What I'm trying to do seems simple, but I've been unable to find a good example either in ESRI documentation or in the Forums.

What I want to do is replace the contents of a text label on a map, using arcpy. Perhaps I'm misunderstanding, but what I have done for this part of the project is to add a text label to the mxd file, using ArcGIS, with a generic piece of text, a label (and name) of "Title," and the text formatting that I want. The arcpy script does quite a few other things, but when it comes time to update the title (with a couple of time/date lines), nothing happens...at all. It doesn't even find the TEXT_ELEMENT that I expected. I've even tried using ...ListLayoutElement(mxd, "TEXT_ELEMENT") in ArcGIS' Python window, where mxd refers to "CURRENT." Nothing. The response is an empty list.

Here's the relevant part of my script:
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
    print "***DEBUG***: text element 'elm.name' = %s" % (elm.name)
    if elm.name == "Title":
        elm.text = elm.text.replace("Title", title)

The "DEBUG" line doesn't show up in the Python output, and the text is not changed on the resulting map later on (which is an exported GIF image).

Thanks in advance if anyone can help out with this.
Tags (2)
0 Kudos
5 Replies
MathewCoyle
Honored Contributor
You say text label, which is not a layout element, it is an element of a feature generally, and not handled by listing layout elements. That you are returning an empty list means something fundamental is going wrong, because even if it can't find your particular text element, any decent layout should have some kind of text elements to find.

Open your mxd of interest in ArcGIS, go to the layout view, click insert and click Text. Enter something like "test text". Run the following.

mxd = arcpy.mapping.MapDocument("CURRENT")
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
    print elm.text
0 Kudos
CherylSharpe
New Contributor
Ah...that explains a lot. I was attempting to use text elements from the Data View. We rarely deal with Layout View, due to the nature of how we currently use GIS data. Now this makes more sense, and I was able to work out the remaining glitches on my own. The whole project now works very nicely.

I thought I was probably missing something fairly basic. Thank you very much!
0 Kudos
ChrisNorth
Esri Contributor
This post explains my problem also.  But - is it possible in python to get access to graphics in the data view?  I want to place a marker on the map at a site location, centre the map there, and also put a fake radius buffer of 250m also centred on the map.  I can manipulate the point and the circle in the layout in page coordinates, but I need to place the graphics using lat/lon coodinates (I already have the graphic at the right radius, I was just going to move the X and Y position).  If I draw the fake buffer in the layout window, I'm pooched if the user changes the scale (zooms in or out).

Can you access elements in the data view with Arcpy Mapping?

Chris
0 Kudos
JeffBarrette
Esri Regular Contributor
Arcpy.mapping does NOT have access to graphics in Data View.

I've done something similar to this in the past.  It was a work around but it worked.  If you know the map coordinates of the corners of your data frame and you know the page coordinates of the corners of your data frame then you can move existing page elements so they align with the appropriate spot on the data frame.

For example, if your X coordinate is 20 percent across the data frame map unit X range, then you set the X page unit of the graphic element to be 20 percent across the data frame element in page units.

The following sample on the Resource Center uses similar logic:
http://resources.arcgis.com/gallery/file/geoprocessing/details?entryID=FFBB051B-1422-2418-88A0-9DD85...

Jeff
0 Kudos
CharlesShore
Emerging Contributor
I'm trying to add a title to an exported pdf.  The output should look similar to the attached file (ignore the 'Clip -- Tile 232')  Instead of the 'Planar' title (which I supplied manually), however, I'd like to be able to supply the title from my pythonaddins (ArcGIS 10.1) code.

The 'Planar' title resides on a TextElement that I added in Layout view.  As shown, this title resides on its own data frame.  The relevant code is as follows:

def createPDF(self):
# Method to obtain truncDateString datestamp
# self.mxdClipLayer is the mxd of interest
        getDateTime()
        pdfString = 'Clip_' + truncDateString[9:15]
        for elm in arcpy.mapping.ListLayoutElements(self.mxdClipLayer, "TEXT_ELEMENT"):
            print elm.   #???
        arcpy.mapping.ExportToPDF(self.mxdClipLayer, envPath + "pdf/" + pdfString + ".pdf")

The problem lies with the print statement.  I'm trying to print elm.name or elm.text, but the Intellisense doesn't provide any property associated with elm. Furthermore, the program fails when I try to force the issue by including 'text' or 'name' as the property.

Thanks in advance for any help you can provide.
0 Kudos