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,793
  • 7
  • 94
  • 195
meryloo
  • 763
  • 1
  • 7
  • 18

1 Answers1

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.

blah238
  • 35,793
  • 7
  • 94
  • 195