I am working on updating a Python 2 script (arcpy.mapping) to a Python 3 script (arcpy.mp).
I've successfully updated the following:
aprxFile=r"G:\usr\Drinking Water Programs\Monroe 2023-02\Maps\ProMap.aprx" #aprx document
arcpy.env.overwriteOutput = True
aprx = Map.ArcGISProject(aprxFile)
layout = aprx.listLayouts('DWP_Maps_5')[0]
noteStat = layout.listElements("TEXT_ELEMENT","Note_statistic")[0]
noteStat.text = "Mapped value is the average for the "+unit
noteTreat = layout.listElements("TEXT_ELEMENT", "Note_treated")[0]
noteTreat.text = "Treated samples not mapped"
noteAny = layout.listElements("TEXT_ELEMENT", "Note_any_sample")[0]
noteAny.text = " "
notePerDist = layout.listElements("TEXT_ELEMENT", "Note_Distribution_per_unit")[0]
notePerDist.text = " "
notePerNum = layout.listElements("TEXT_ELEMENT", "Note_Numbers_per_unit")[0]
notePerNum.text = " "
The next part not so successfully:
dfList = aprx.listMaps(aprx)
baseMCD1 = dfList.listLayers("MCD_light") #new code
baseMCD2 = Map.listLayers(aprx,"MCD_medium",dfList[0])[0] #old code
I get the following error:
AttributeError Traceback (most recent call last) In [37]: Line 84:
baseMCD1 = dfList.listLayers("MCD_light")AttributeError: 'list' object has no attribute 'listLayers'
arcpy.mapping
sytax witharcpy.mp
. For example,aprx.listMaps(aprx)
isn't going to return any results. Even after you change the line todfList = aprx.listMaps()
, the next line of code still won't work becauselistMaps()
returns a Python list of Maps in the APRX, and Python lists have nolistLayers
method, hence the error. You need to either select a map from the Python list or iterate over the whole list and call retrieve the layers of the individual Map objects.