I have a script that I am trying to move over to Pro but I am having problems with updating text the layout .
The text "Template" in the layout doesn't get updated by arcpy.GetParameterAsText. I don't know why it doesn't update, I don't get any errors.
import arcpy, string, os
aprx = arcpy.mp.ArcGISProject(r"C:\Temp\ArcGISProTest.aprx")
#acc_val = arcpy.GetParameterAsText(0)
project = arcpy.mp.ArcGISProject("CURRENT")
layer1 = project.listMaps()[0]
lyr = layer1.listLayers("TAXLOTS")[0]
lyt = project.listLayouts("Layout1")[0]
mf = lyt.listElements('MAPFRAME_ELEMENT','*')[0]
arcpy.env.workspace = os.path.dirname(project.filePath)
wp = os.path.dirname(project.filePath)
values = arcpy.GetParameterAsText(0)
fieldName = "ParID"
values = values.split(";") # split values into list
values = ["'{0}'".format(v) for v in values] # add single quotes
whereClause = "{0} IN ({1})".format(fieldName, ",".join(values))
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", whereClause)
Name = arcpy.GetParameterAsText(1)
for lyt in aprx.listLayouts():
for elm in lyt.listElements("TEXT_ELEMENT"):
if elm.text == "Template":
elm.text = Name
with arcpy.da.SearchCursor(lyr, ["SHAPE@","ParID"]) as cursor:
for row in cursor:
featureextent = mf.getLayerExtent(lyr)
# Set mapframe camera to featureextent
mf.camera.setExtent(featureextent)
# Reduce mapframe slightly to avoid feature clipping.
mf.camera.scale *= 1.2
#arcpy.RefreshActiveView()
vals = "1;2;3".split(";")
values = ["'{0}'".format(v) for v in vals]
values # ---- hmmmmm
["'1'", "'2'", "'3'"]
# ---- try again
values = ["{0}".format(v) for v in vals]
values
['1', '2', '3']
That part of my codes works fine, with your recommendation gives me an error.
values = ["{0}".format(v) for v in vals]
ERROR 000358: Invalid expression.Failed to execute (SelectLayerByAttribute).
maybe you have empty selection so nothing to split . It is safer to be overt and check and preassign conditions that may be encountered. A couple of extra lines of code are also easier to track errors since "None" is not an iterable, so it will fail in the "for v in vals" section.
vals = "1;2;3".split(";")
if vals:
out = ["{0}".format(v) for v in vals]
else:
out = []
out
['1', '2', '3']
vals = None
out
[]
Always show some sample data which includes "regular" and "edge" cases
That part of the code works fine, the parcel ParID's are selected and it zooms to the ParID's.
The text element in the layout doesn't get updated by the arcpy.GetParameterAsText.
This part doesn't do anything and I don't get any error.
Name = arcpy.GetParameterAsText(1)
for lyt in aprx.listLayouts():
for elm in lyt.listElements("TEXT_ELEMENT"):
if elm.text == "Template":
elm.text = Name
test it to see what you are getting then
for elm in lyt.listElements("TEXT_ELEMENT"):
print(elm.text)
if elm.text == "Template":
I had to call the text element name and in my case it was 'Text 1' not the 'Template', like below.
import arcpy
Name = arcpy.GetParameterAsText(1)
aprx = arcpy.mp.ArcGISProject("CURRENT")
for lyt in aprx.listLayouts():
for elm in lyt.listElements("TEXT_ELEMENT", "Text 1"):
elm.text = Name
Which goes to show the usefulness of print statements when debugging