Solved! Go to Solution.
import arcpy, os from arcpy import mapping as m # Folder containing MXD's folder = r'C:\path\to_your\mapdocuments' arcpy.env.workspace = folder # Find and Replace text element find_elm = 'Text you want to find' replace_elm = 'Text you want to replace find_elm' # Find all .mxd files in folder # replaces text element in mxd if it exists for mapDoc in arcpy.ListFiles('*.mxd'): mxd = arcpy.mapping.MapDocument(os.path.join(folder, mapDoc)) for elm in arcpy.mapping.ListLayoutElements(mxd, 'TEXT_ELEMENT'): if elm.text == find_elm: elm.text = replace_elm print 'Replaced {0} in {1}'.format(find_elm, mapDoc) mxd.save() del mxd
import arcpy, os from arcpy import mapping as m # Folder containing MXD's folder = r'C:\path\to_your\mapdocuments' arcpy.env.workspace = folder # Find and Replace text element find_elm = 'Text you want to find' replace_elm = 'Text you want to replace find_elm' # Find all .mxd files in folder # replaces text element in mxd if it exists for mapDoc in arcpy.ListFiles('*.mxd'): mxd = arcpy.mapping.MapDocument(os.path.join(folder, mapDoc)) for elm in arcpy.mapping.ListLayoutElements(mxd, 'TEXT_ELEMENT'): if elm.text == find_elm: elm.text = replace_elm print 'Replaced {0} in {1}'.format(find_elm, mapDoc) mxd.save() del mxd
Thanks Caleb - this worked but I'd like your advice on modifying based on two scenarios:
1) How do I find/replace a single word instead of the entire text string?
2) It doesn't work if I have two rows of text. My text block says: Figure 1 (row 1) Project Overview (row 2).
My code says: if elm.text == "Figure 1 Project Overview": Everything works fine when only one row of text it present but multiple lines do not work.
Thanks,
Jason
if elm.text == "Figure 1 Project Overview\nYour next line to replace here": elm.text = "First replace line\nSecond Replace Line"
if elm.text.startswith("Figure 1 Project Overview\nYour next line to replace here"): elm.text = "First replace line\nSecond Replace Line"
find_word = 'test' replace_word = 'replace' for elm in arcpy.mapping.ListLayoutElements(mxd, 'TEXT_ELEMENT'): if find_word in elm.text: elm.text = elm.text.replace(find_word, replace_word) print 'Replaced {0} in {1}'.format(elm.text, mapDoc)
if elm.text.startswith("Figure 1 Project Overview"): elm.text = "First replace line\nSecond Replace Line"