2

I would like to selectively choose my MXD documents through the multivalue or filter parameter in the toolbox.

From the selected MXD's the user should be able to replace text and re-save the document.

I can get it to work on a series of unselected MXD documents located in a specific folder, but I need to be able to choose those MXD's.

Essentially the question is how do I integrate the multi-value parameter into the code?

import arcpy
import os

inMXD = arcpy.GetParameterAsText(0) #toolbox parameter workspace
#prepare for multivalue toolbox parameter
#ListInMXD = inMXD.split(";")
#set old and new text variables
OldText = arcpy.GetParameterAsText(1)
NewText = arcpy.GetParameterAsText(2)

#THis is where it should loop through the ListInMXD 

for file in ListInMXD:
    filePath = os.path.join(ListInMXD, file)
    mxd = arcpy.mapping.MapDocument(filePath)
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
        if elm.text == OldText:
            elm.text = NewText
            mxd.save()

With PolyGeo's answer I was able successfully to re-write the code as follows:

import arcpy
import os
mxdString = arcpy.GetParameterAsText(0) #toolbox parameter workspace
#prepare for multivalue toolbox parameter
mxdList = mxdString.split(";")
arcpy.AddMessage(mxdList)
#set old and new text variables
OldText = arcpy.GetParameterAsText(1)
NewText = arcpy.GetParameterAsText(2)
#THis is where it should loop through the ListInMXD 
for mxdItem in mxdList:
    mxd = arcpy.mapping.MapDocument(mxdItem.strip("'")) #this little snippet came from https://geonet.esri.com/thread/67229
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
        if elm.text == OldText:
            elm.text = NewText
    mxd.save()
del mxd
5

To create a multi-value input of MXD files in a tool dialog and create a Python list from them you can do the following:

  1. Write a short script like:

    import arcpy

    mxdString = arcpy.GetParameterAsText(0)

    mxdList = mxdString.split(";")

    arcpy.AddMessage(mxdList)

  2. Add the Script above to a toolbox with its parameter defined as below with

    • MultiValue: Yes;
    • Filter: File; and
    • Extensions: mxd

enter image description here

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.