0

I have a problem updating a map element text (named element1) in a map document using the following code:

a1 = '"' + ("element" + str(1)) + '"'
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
if elm.name  == a1:
    elm.text = str("new text")
    elm.elementPositionX = 0.6046
    elm.elementPositionY = 6.4636

The code above does not update the element1 with the new text value.

It looks like the following part does not work:

if elm.name  == a1:

Because when using the below hard coded approach:

if elm.name  == "element1":

It worked, but I need to have it as variable.

What am I doing wrong here?

3
  • replace <<a1 = '"' + ("element" + str(1)) + '"'>> by a1 = "element" + str(1) , should work – geogeek Feb 24 '13 at 23:13
  • 1
    'element1' == "element1" != '"element1"' – Mike T Feb 25 '13 at 3:11
  • many thanks for your help - the geogeek's hint worked a trick for me! and thanks Mike - all understood :) – meryloo Feb 25 '13 at 8:15
2

You don't need to wrap the element name in quotes like you are doing.

You can use string formatting to do this slightly more cleanly:

a1 = "element{0}".format(1)

See also my suggestions on general Python tips in this answer.

Not the answer you're looking for? Browse other questions tagged or ask your own question.