I'm using ArcGIS Pro 3.2 and I'm trying to use arcpy.mp and the layout CIM to update a scale bar's units.
This is what I have so far, but the units somehow get erased from the layout cim json instead of updated:
import arcpy
aprx = arcpy.mp.ArcGISProject("CURRENT")
vicinityLayout = aprx.listLayouts("Fig1_Vicinity")[0]
# Update scale bar units to miles
scaleBarName = 'Scale Bar'
vicinityLayoutCIM = vicinityLayout.getDefinition("V3") # Get the layout CIM
## Find the Scale Bar - updating the units for the
jsonText = "{\"uwkid\":9093}"
for elm in vicinityLayoutCIM.elements:
if elm.name == "Scale Bar":
elm.unitLabel = "Miles"
elm.numberFormat.roundingValue = 0
elm.units = jsonText
vicinityLayout.setDefinition(vicinityLayoutCIM)
Solved! Go to Solution.
Found the solution. I needed to convert the unit string to json. Not just make it look like json. This worked!
import arcpy, json
aprx = arcpy.mp.ArcGISProject("CURRENT")
vicinityLayout = aprx.listLayouts("Fig1_Vicinity")[0]
# Update scale bar units to miles
scaleBarName = 'Scale Bar'
vicinityLayoutCIM = vicinityLayout.getDefinition("V3") # Get the layout CIM
## Find the Scale Bar
jsonText = "{\"uwkid\":9093}" # need to convert this to json to make the unit update
scalebarUnit = json.loads(jsonText)
for elm in vicinityLayoutCIM.elements:
if elm.name == "Scale Bar":
elm.unitLabel = "Miles"
elm.numberFormat.roundingValue = 0
elm.units = scalebarUnit
vicinityLayout.setDefinition(vicinityLayoutCIM)
Found the solution. I needed to convert the unit string to json. Not just make it look like json. This worked!
import arcpy, json
aprx = arcpy.mp.ArcGISProject("CURRENT")
vicinityLayout = aprx.listLayouts("Fig1_Vicinity")[0]
# Update scale bar units to miles
scaleBarName = 'Scale Bar'
vicinityLayoutCIM = vicinityLayout.getDefinition("V3") # Get the layout CIM
## Find the Scale Bar
jsonText = "{\"uwkid\":9093}" # need to convert this to json to make the unit update
scalebarUnit = json.loads(jsonText)
for elm in vicinityLayoutCIM.elements:
if elm.name == "Scale Bar":
elm.unitLabel = "Miles"
elm.numberFormat.roundingValue = 0
elm.units = scalebarUnit
vicinityLayout.setDefinition(vicinityLayoutCIM)