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?

blah238
35.9k8 gold badges97 silver badges204 bronze badges
asked Feb 24, 2013 at 23:05
3
  • replace <<a1 = '"' + ("element" + str(1)) + '"'>> by a1 = "element" + str(1) , should work Commented Feb 24, 2013 at 23:13
  • 1
    'element1' == "element1" != '"element1"' Commented Feb 25, 2013 at 3:11
  • many thanks for your help - the geogeek's hint worked a trick for me! and thanks Mike - all understood :) Commented Feb 25, 2013 at 8:15

1 Answer 1

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.

answered Feb 25, 2013 at 0:22

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.