As in the title, I have the simple below code:

import arcpy

arcpy.env.workspace = r"U:"
mxd = arcpy.mapping.MapDocument(r"U:RAFT.mxd")
layoutlist = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "")

print layoutlist

Which gives me:

[<TextElement object at 0x23ddb50[0x15325228]>, <TextElement object at 0x23dd990[0x1531c548]>, <TextElement object at 0x15311570[0x1531c570]>, <TextElement object at 0x15311bd0[0x15325340]>, <TextElement object at 0x23ddb30[0x153252a0]>, <TextElement object at 0x15311710[0x1531c6d8]>]

Which lists the Text Elements in the mxd as expected. However, I have given one text element a name as it is the specific one I want to work with. My assumption was that element name should appear in the list? How can I do this?


The cryptic "names" of each text element you see in the list, such as <TextElement object at 0x23ddb50[0x15325228]> is just a pointer to a memory address and not the actual name of the text element you give within a map document.

To be able to list the names of a text element, you'd need to use its name property as below:

[elem.name for elem in layoutlist]

Sometimes you want to find a particular text element by its name, for instance, to change its position or text. You could do this by checking the name of each text element present as below:

text_elem_to_work_with = [elem for elem in layoutlist if elem.name == 'elem_name_to_search'][0]

This would give you a single TextElement you can inspect or mutate as needed.

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.