I'm looking to shift a scale bar with Python, and as such have a small proof of concept script happening in a python console window. Using the below code, no matter the location given with elementPositionY, the scale bar does not seem to change location at all.
My code:
mxd = arcpy.mapping.MapDocument(r"C:\users\I5-Desktop\Documents\untitled.mxd") #current mxd
scaleBar = arcpy.mapping.ListLayoutElements(mxd,"MAPSURROUND_ELEMENT","ScaleBar1") #enforce working on "ScaleBar1". Can use wildcards when we have more.
for item in scaleBar:
print item.name #confirm that we are working on the only scalebar that's currently in the document
if item.name == "ScaleBar1":
item.elementPositionY = 1
print item.name #reconfirm we have entered the if statement with the name
What reason might there be for the scalebar not changing location? I can resolve its name, and enter a loop based on its name, etc.
everything points to saving the mxd since you aren't working with 'CURRENT', otherwise, there arcpy.RefreshActiveView
Thanks Dan
Neither saving nor using arcpy.RefreshActiveView after the posted code seems to make a difference. That said, there's no errors either. RefreshActiveView returns <function RefreshActiveView at 0x255B32B0> which seems ok.
you need... RefreshActiveView() # the brackets
Thanks Dan.
Although this didn't work for me in this instance, I need to learn where I require brackets and where I don't. Rookie error. Thanks.
As it turns out, and for whatever reason, I needed to be using CURRENT and not a reference to the mxd. The brackets were similarly important.
So, the working code is:
mxd = arcpy.mapping.MapDocument("CURRENT") #current mxd
scaleBar = arcpy.mapping.ListLayoutElements(mxd,"MAPSURROUND_ELEMENT","ScaleBar1") #enforce working on "ScaleBar1". Can use wildcards when we have more.
for item in scaleBar:
print item.name #confirm that we are working on the only scalebar that's currently in the document
if item.name == "ScaleBar1":
item.elementPositionY = 1
arcpy.RefreshActiveView()
print item.name #reconfirm we have entered the if statement with the name
As suspected... glad it worked out