TexElementObject: Error in setting text

996
4
08-19-2013 11:21 AM
RobertMartin2
Occasional Contributor II
I'm trying to write a script to update a TextElement object in a batch of MXDs. (I know there's a similar discussion going on right now but I didn't see anything relevant to my issue.) So far I've been getting the error:

RuntimeError: TextElementObject: Error in setting text


This is my code:

import os
import arcpy

# Loop over MXDs
root = r'H:\myDir'

for file in os.listdir(root):
 print 'Working on ' + file
 path = os.path.join(root, file)
 mxd = arcpy.mapping.MapDocument(path)
 text_elms = arcpy.mapping.ListLayoutElements(mxd, 'TEXT_ELEMENT')
 
 the_text_elm = None

 # Loop over text elements to find subtitle
 for text_elm in text_elms:
  i = text_elm.text.find('Archit')
  if i > -1:
   the_text_elm = text_elm
   break
 
        # Update
 if the_text_elm:
  i = the_text_elm.text.find('Archit')
  new_text = the_text_elm.text[:i] + 'Historic Resources'
  the_text_elm.text =  new_text
  mxd.save()
 else:
  print 'Did not update'


I've checked every step of the routine up until the text is set and everything looks fine (no unexpected nulls). Can anyone see anything wrong with what I'm doing here?

Thanks!
Tags (2)
0 Kudos
4 Replies
MathewCoyle
Frequent Contributor
Your code runs without error for me. What version of ArcGIS are you using?
0 Kudos
RobertMartin2
Occasional Contributor II
I'm on 10 SP5. Interesting that it runs fine for you - now I'm wondering if it's something with the MXD.
0 Kudos
RobertMartin2
Occasional Contributor II
Sure enough, I skipped the MXD that was throwing the error and it went on fine. I ran the MXD Doctor on it and still no dice, but one manual edit won't kill me 🙂

Thanks again.
0 Kudos
RhettZufelt
MVP Frequent Contributor
It does not run for me at all. In fact, I set root = r'D:' and it gives me .prj files located several folders deep. In no way can I make it report the mxd's in the listed directory.

If I start it out like this it will get the list of mxd's:

arcpy.env.overwriteOutput = True 

text_elms = []
the_text_elm = None

start = "D:\\"
for root, dirs, files in os.walk(start):
    for mapDoc in files:
        if mapDoc.endswith(".mxd"):
            print 'Working on ',mapDoc
            path = os.path.abspath(os.path.join(root,mapDoc))
            mxd = arcpy.mapping.MapDocument(path)
            text_elms = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "Archit")[0]
            the_text_elm = None
            the_text_elm = text_elm



have you typed print os.listdir(root) and make sure that it only reports mxd files? as mine crashes trying to set the mxd = somefileotherthan mxd (the first file in my directory) the above code "fixes" that.

If you are changing for ALL text elements with name = "Archit", you don't need to iterate through the list
In fact, you assign it to text_elms, then try to iterate through that as a list.
Can't be done as text_elms is a list of TextElement objects, and is not iterable as is not a list ().

Then, in this part, do whatever if's you need, but you have assigned the text element to text_elms variable (not the_text_elm), so that is the one you want to update:

 if the_text_elm:
  i = the_text_elm.text.find('Archit')
  new_text = the_text_elm.text[:i] + 'Historic Resources'
  text_elms.text =  new_text
  mxd.save()




Here is modified code that is working for me, you should be able to extract what you need:


import os
import arcpy


# Overwrite pre-existing files
arcpy.env.overwriteOutput = True

# Loop over MXDs
folder = r'H:\myDir'
arcpy.env.workspace = folder
arcpy.env.overwriteOutput = True 

for root, dirs, files in os.walk(start):
    for mapDoc in files:
        if mapDoc.endswith(".mxd"):
            print 'Working on ',mapDoc
            path = os.path.abspath(os.path.join(root,mapDoc))
            mxd = arcpy.mapping.MapDocument(path)
            if arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "Archit"):   # this keeps it from erroring out if the text element "Archit" doesn't exist...
               text_elms = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "Archit")[0]
               new_text = "test"  + ' Historic Resources'
               text_elms.text =  new_text
            mxd.save()


Keep in mind this will walk through all sub-folders as well.

Hope this gets you on the right track,

R_
0 Kudos