Migrate script tool with parameters to ArcGIS Pro

559
3
01-05-2021 04:18 PM
Brian_McLeer
Occasional Contributor II

I have a script from an ArcMap toolbox that updates text elements in the MXD as user input parameters below. I am trying to update this to work in Pro, but I am having trouble finding resources. I have tried updating certain codes in the second snippet below with text in bold. 

Original:

import arcpy 
mxd = arcpy.mapping.MapDocument("CURRENT")

# Defines Address Element Name
SiteName = arcpy.GetParameterAsText(0).upper()
Address = arcpy.GetParameterAsText(1).upper()
PreplanID = arcpy.GetParameterAsText(2).upper()
CrossStreet = arcpy.GetParameterAsText(3).upper()
Floor = arcpy.GetParameterAsText(4).lower()

# Autopopulate Remaining Text Elements from User Inputs and Cursor search
for elm in arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT"):
if elm.name == 'SiteName':
elm.text = SiteName
elif elm.name == 'Address':
elm.text = Address
elif elm.name == 'PreplanID':
elm.text = PreplanID
elif elm.name == 'CrossStreet':
elm.text = CrossStreet
elif elm.name == 'Floor':
elm.text = Floor

 

Failing update:

import arcpy 
aprx = arcpy.mp.ArcGISProject("CURRENT")

# Defines Address Element Name
SiteName = arcpy.GetParameterAsText(0).upper()
Address = arcpy.GetParameterAsText(1).upper()
PreplanID = arcpy.GetParameterAsText(2).upper()
CrossStreet = arcpy.GetParameterAsText(3).upper()
Floor = arcpy.GetParameterAsText(4).lower()

# Autopopulate Remaining Text Elements from User Inputs and Cursor search
for elm in arcpy.mp.ListLayoutElements(aprx,"TEXT_ELEMENT"):
if elm.name == 'SiteName':
elm.text = SiteName
elif elm.name == 'Address':
elm.text = Address
elif elm.name == 'PreplanID':
elm.text = PreplanID
elif elm.name == 'CrossStreet':
elm.text = CrossStreet
elif elm.name == 'Floor':
elm.text = Floor

 

Brian
0 Kudos
3 Replies
MarkBryant
New Contributor III

Because Pro has multiple layouts, you need to get the layout as well.

Try something like this:

aprx = arcpy.mp.ArcGISProject("CURRENT")
lyt = aprx.listLayouts("your_layout_name")[0]
for elm in lyt.listElements("TEXT_ELEMENT"):
  if elm.name == 'SiteName':
    elm.text = SiteName 
  elif elm.name == 'Address':
     elm.text = Address


 

Mark.
0 Kudos
Robert_LeClair
Esri Frequent Contributor

I'd recommend you run the  Analyze Tools for Pro GP tool on your script to see what part of the Python script will need to be changed from 2.7 Python to 3.7.x Python.  The GP tool is available in ArcMap and ArcGIS Pro.

0 Kudos
MahmoudHegazy
New Contributor III

i have the same issues and want to convert the script from arcmap to arcgis pro 

import arcpy, os

arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")

df = arcpy.mapping.ListDataFrames(mxd)[0]

layerName = arcpy.GetParameterAsText(0)
fieldName = arcpy.GetParameterAsText(1)
imgLocation = arcpy.GetParameterAsText(2)
whereList = []

layerFile = arcpy.mapping.Layer(layerName)
layerFile.definitionQuery = ""

print df.name


with arcpy.da.SearchCursor(layerName, fieldName) as cursor:
    for row in cursor:
        for item in row:
            whereList.append(item)

for wl in whereList:

    arcpy.RefreshActiveView()
    mxd.save()   
    layerFile.definitionQuery = fieldName + "= '" + wl + "'"
    new_extent = layerFile.getExtent()
    df.extent = new_extent
    df.scale *= 1.5
    
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "title"):      
    
        if elm.name == "title":
           elm.text = wl

    arcpy.RefreshActiveView()
    outFile = os.path.join(imgLocation, wl + ".pdf")
    arcpy.AddMessage(outFile)
    outFile = imgLocation + "\\" + wl + ".pdf"   
    arcpy.mapping.ExportToPDF(mxd, outFile, "PAGE_LAYOUT")
del mxd
0 Kudos