0

I've been able to add a dynamic text element (with an element name of 'TestName') that reflects a layer name and that changes for each corresponding exported layer to a GIF. I am now trying to add a second text element (with an element name of 'TestName2') for a map title but am running into issues. Below is the code I am working with. CMD prompt produces this error:

C:\UserS\JameS>export5.py 
[<map layer u’Div1_Irrig_1987_2’>, <map layer u’Div1_Irrig_1987_2’>, <map layer 
u’Div1_Irrig_1956_0’>, <map layer u’citylyr’>, <map layer u’boundlyr’>]
Traceback (most recent call last):
   File "C:\Python27\ArcGIS10.2\Scripts\TestScripts\export5.py”, line 42, in <module>
   elm.text : "" 
   File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\arcobjects\7base.py", line 89, in _set
      return setattr(self._arc_object, attr_name, cval(val))
RuntimeError: TextElementObject: Error in setting text

C:\Users\James>

enter image description here

for lyr in lyrlist:
        lyr.visible = False
        #if lyr.name in layers and lyr.name <> layer:
        #   lyr.visible = False
        if lyr.name == "citylyr":
            lyr.visible = True
        if lyr.name == "boundlyr":
            lyr.visible = True
        if lyr.name == name:
            lyr.visible = True
            layerOn = lyr.name

            for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
                if elm.name == 'TestName':
                    elm.text = lyr.name
                if elm.name == 'TestName2':
                    elm.text = 'Example Map Title Name'
                else:
                    elm.text = ""

    if layerOn:
        arcpy.mapping.ExportToGIF(mxd,r"{}\{}.gif".format(GIFPath,layerOn))
  • What version of ArcGIS are you using? Cloning of text elements only became available in ArcPy a few versions back. Please always include errors as text rather than pictures. – PolyGeo Mar 10 '16 at 9:30
1

The issue appears to be in your else: - it's trying to set your elm.text even if there isn't an option for text. You don't need to set elm.text if there's no text to change/remove in that element.

I'd also move the for elm... loop in with the export just to keep things tidier (and easier to read).

elements = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT")
for layer in layers:
    layerOn = ""
    for lyr in lyrlist:
            lyr.visible = False
            #if lyr.name in layers and lyr.name <> layer:
            #   lyr.visible = False
            if lyr.name == "citylyr":
                lyr.visible = True
            if lyr.name == "boundlyr":
                lyr.visible = True
            if lyr.name == name:
                lyr.visible = True
                layerOn = lyr.name

    if layerOn:
        for elm in elements:
            if elm.name == 'TestName':
                elm.text = layerOn
            if elm.name == 'TestName2':
                elm.text = 'Example Map Title Name'
        arcpy.mapping.ExportToGIF(mxd,r"{}\{}.gif".format(GIFPath,layerOn))

I have re-added a couple of code lines from your original question so that the added elements... line is added in context.

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.