5

I found some code on a website that will perform a search and replace on page layout text elements. Currently you can only do it one MXD at a time. I was hoping to change the code so I can do a search and replace on a folder with a bunch of MXDs in it.

The script is below. I know I need a loop, just not sure how to do it; never programmed before.

import arcpy, string, os 

#Read input parameters from script tool
Path = arcpy.GetParameterAsText(0)
oldText = arcpy.GetParameterAsText(1)
newText = arcpy.GetParameterAsText(2)
case = arcpy.GetParameter(3)
exact = arcpy.GetParameter(4)
outputMXD = arcpy.GetParameterAsText(5)

try:
    #Referent the map document
    mxd = arcpy.mapping.MapDocument(mxdPath)         

    #Find all page layout text elements
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):     
        if exact:
            if case:
                if oldText == elm.text:
                    elmText = elm.text.replace(oldText, newText)
                    elm.text = elmText
            else:
                if oldText.upper() == elm.text.upper():
                    elmText = elm.text.upper().replace(oldText, newText)
                    elm.text = elmText   
        else:
            if case:
                if oldText in elm.text:
                    elmText = elm.text.replace(oldText, newText)
                    elm.text = elmText
            else:
                if oldText.upper() in elm.text.upper():
                    elmText = elm.text.upper().replace(oldText, newText)
                    elm.text = elmText                  
    mxd.saveACopy(outputMXD)

    del mxd

except Exception, e:
    import traceback
    map(arcpy.AddError, traceback.format_exc().split("\n"))
    arcpy.AddError(str(e))
6

The Updating and fixing data sources with arcpy.mapping help topic has an example of how to loop through .mxd's in a folder (as well as other useful arcpy examples) but my preference for how to do this would be to use glob. Something like this:

import glob
mxdList = glob.glob('*.mxd')
for mxd in mxdList:
    <put everything from your try/except block here>

This assumes your script runs from the folder where you have all your .mxd's. You could also organize this a bit better with a few functions but that's probably beyond the scope of this question.

1
#This script will search through all mxds in a folder
#and will search and replace multi-line text string within each mxd and
#print out a new pdf

#Notes:
#The "Old text" must match exactly to the existing text to be
#replaced, with no "white space" before or after the text. Otherwise, the script will not #work. Best approach is to copy the text from the mxd(s) to be edited and paste it to the #script where "old text" variable is located.

#For multi-line text, be sure to include the "\r\n" string.
import arcpy
import os
import glob

folder = ws = "S:\\GIS\\xtemp"

#Enter all EXACT cases of text to change below- remember to include "\r\n" where next #line is needed:

oldtext = "Corte Madera Creek Flood Risk Management\r\nCorte Madera, Marin County, CA"
newtext = "Corte Madera Creek Flood Risk Management Project\r\nMarin County, CA"

#This is only used to see how the text will be written out in the final output:
print "old text:" + oldtext
print "newtext:" + newtext

print "Searching mxds in:" + folder

#This script part will allow a search of all mxds in the folder:
mxd = arcpy.mapping
mxds = glob.glob(folder + '\\' + '*.mxd')
arcpy.gp.overwriteOutput = True

for mxdFile in mxds:
    mxd = arcpy.mapping.MapDocument(mxdFile)
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
        if elm.text == oldtext:
            elm.text = newtext
    mxd.save()
    del mxd       
print "Text replacement complete - printing new pdfs to:" + folder

#This script part will print the final pdfs after changes have been made:
Workspace = ws
arcpy.env.workspace = Workspace
arcpy.env.overwriteOutput=True
for file in arcpy.ListFiles("*.mxd"):
    mxd_path = os.path.join(Workspace,file)
    mxd = arcpy.mapping.MapDocument(mxd_path)
    outpdf = file + ".pdf"
    arcpy.mapping.ExportToPDF(mxd,outpdf)
    mxd.save()
print "Finished! New pdfs printed to:" + folder

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.