convert script from arcmap env to arcgis pro env

712
7
09-18-2021 06:35 AM
Labels (1)
MahmoudHegazy
New Contributor III

 

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
7 Replies
DanPatterson
MVP Esteemed Contributor

What line was your first error encountered on?

ArcGIS Pro Python reference—ArcGIS Pro | Documentation

as a start...

hints

mapping --->  mp

print "blah" ---> print("blah")

work your way through the code so you can learn the translation as you go

 


... sort of retired...
0 Kudos
MahmoudHegazy
New Contributor III

i don't know what is the erorr exactly  line 31
for lyt in aprx.listLayouts("Layout"): SyntaxError: invalid syntax

import arcpy, os

arcpy.env.overwriteOutput = True
aprx = arcpy.mp.ArcGISProject("CURRENT")
df = aprx.listMaps()[0]

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

layerFile = df.listLayers(layerName)[0]
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:      
    layerFile.definitionQuery = fieldName + "= '" + wl + "'"
    #new_extent = layerFile.getExtent()
    lay = aprx.listLayouts("Layout")
    mf = lay.listElements("Layers Map Frame","New Data Frame Map Frame")
    new_extent = mf.camera.setExtent(mf.getLayerExtent(layerFile, False, True)
    #df.extent = new_extent
    #df.scale *= 1.5
    for lyt in aprx.listLayouts("Layout"):
        for elm in lyt.listElements("TEXT_ELEMENT"):
            if elm.name == "Text":
                elm.text = wl          
0 Kudos
MahmoudHegazy
New Contributor III

syntax error in line 31:

 

 

import arcpy, os

arcpy.env.overwriteOutput = True
aprx = arcpy.mp.ArcGISProject("CURRENT")
df = aprx.listMaps()[0]

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

layerFile = df.listLayers(layerName)[0]
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:      
    layerFile.definitionQuery = fieldName + "= '" + wl + "'"
    #new_extent = layerFile.getExtent()
    lay = aprx.listLayouts("Layout")
    mf = lay.listElements("Layers Map Frame","New Data Frame Map Frame")
    new_extent = mf.camera.setExtent(mf.getLayerExtent(layerFile, False, True)
    #df.extent = new_extent
    #df.scale *= 1.5
    for lyt in aprx.listLayouts("Layout"):
        for elm in lyt.listElements("TEXT_ELEMENT"):
            if elm.name == "Text":
                elm.text = wl          

 

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

missing a closing bracket in

new_extent = mf.camera.setExtent(mf.getLayerExtent(layerFile, False, True)

should be

new_extent = mf.camera.setExtent(mf.getLayerExtent(layerFile, False, True))


... sort of retired...
MahmoudHegazy
New Contributor III

after i put the bracket it showed me this message error

AttributeError: 'list' object has no attribute 'listElements'

import arcpy, os

arcpy.env.overwriteOutput = True
aprx = arcpy.mp.ArcGISProject("CURRENT")
df = aprx.listMaps()[0]

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

layerFile = df.listLayers(layerName)[0]
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:      
    layerFile.definitionQuery = fieldName + "= '" + wl + "'"
    #new_extent = layerFile.getExtent()
    lay = aprx.listLayouts("Layout 1")
    mf = lay.listElements("Layers Map Frame","New Data Frame Map Frame")
    new_extent = mf.camera.setExtent(mf.getLayerExtent(layerFile, False, True))
    #df.extent = new_extent
    #df.scale *= 1.5
    for lyt in aprx.listLayouts("Layout 1"):
        for elm in lyt.listElements("TEXT_ELEMENT"):
            if elm.name == "Text":
                elm.text = wl          
     
    name = ''
    for char in wl:
        if char.isalpha():
            name+=char

    outFile = imgLocation + "\\" + name  + ".pdf"   
    arcpy.mp.ExportToPDF(aprx, outFile, "PAGE_LAYOUT")
del aprx

 

the two data frame called as you see in the screen shot blow :

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

TextElement—ArcGIS Pro | Documentation  has some code examples, you may need to examine the required workflow


... sort of retired...
DonMorrison1
Occasional Contributor III

It looks to me list you need to add another loop - to process the list of layouts that your get from the call to listLayouts.  Like this

for lyts in aprx.listLayouts("Layout 1"):
    for lyt in lyts:
        for elm in lyt.listElements("TEXT_ELEMENT"):
            if elm.name == "Text":
                elm.text = wl