I need to be able to walk through many mxds to replace text strings but able to skip a character place. For example, replace:

'area %d'


'area %d - this is some other text'


'area 2'

'area 2 - this is some example text'

'area 3'

'area 3 - this is some example text'

This is what I have so far. It works for replace a specific string but I have not been able to figure out the placeholder variable.

import os
import arcpy

mxd_path = r'C:\Users\username\Desktop\test'

for dirpath, dirnames, filenames in os.walk(mxd_path):
    for filename in filenames:
        fullPath = os.path.join(dirpath, filename)
        basename, extension = os.path.splitext(filename)
        if extension.lower() == ".mxd":
            has_data_source = False
            mxd = arcpy.mapping.MapDocument(fullPath)
            for elm in arcpy.mapping.ListLayoutElements(mxd, 'TEXT_ELEMENT'):
                elm.text = elm.text.replace('area %d',
                                            'area %d - this is some other text')

            del mxd
  • Is it always the same text you're skipping? or in exactly the same position? Please give us more information about what you're skipping and why. Is it always a single character? – Midavalo Dec 22 '17 at 16:04
  • It is always in the same position and its always a number. anywhere from 1 to 20. I have to change titles a lot on many mxds andIt would be faster to automate then open each on up. – ajd018 Dec 22 '17 at 16:14
  • Why not just 'area 1' + ' blablabla'? Or '{0} {1}'.format(areastring, newstring) – BERA Dec 22 '17 at 18:33

If you are just adding text to the end of an existing string, then just concatenate text onto the end:

elm.text = "{0} - Additional text goes here".format(elm.text)

So Area 1 becomes Area 1 - Additional text goes here

  • This worked great. – ajd018 Dec 22 '17 at 19:49

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.