-2

This question already has an answer here:

I'm using Desktop 10.2 and I tried with this script to add new text element "road 90" to a lot of mxd's, but Something wrong with the code:

import arcpy
from arcpy import env 


env.workspace = r"F:\Projects\Nadav\road90\gis"
for mxdname in arcpy.ListFiles("*.mxd"):
    print mxdname
    mxd = arcpy.mapping.MapDocument(r"F:\Projects\Nadav\road90\gis\\" + mxdname)
    for TextElement in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
        TextElementName = "road 90"
        if TextElementName == "TextElementName":
            true
            print 'text'
    mxd.save()
del mxd

marked as duplicate by Jason Scheirer, PolyGeo, Erica, Dan C, Chris W Aug 17 '14 at 17:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3

In your MXD, check the Size and Position tab of your text element Properties, and if you make its name be called say "TextElement1" then changing:

for TextElement in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
    TextElementName = "road 90"
    if TextElementName == "TextElementName":
        true
        print 'text'

in your code to be:

for TextElement in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
    if TextElement.name == "TextElement1":
        TextElement.text = "My new Text Element text"

should cause a change that you can detect and adapt to your precise requirements.

Note that the above will update an existing text element and that you will always need to have at least one text element in your MXD to begin with.

You can clone an existing text element in an MXD and then modify that if you wish - see TextElement (arcpy.mapping) help:

Provides a mechanism to clone an existing graphic text on a page layout.

If you do not want to clone an existing text element then your only option will be to try for an enhancement to ArcPy via ArcGIS Ideas.

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