I try to change the font size and the element xy position of text element for several maps. Right now the font size is 21.79 and the xy position is:
elm.elementPositionX = 8.204
elm.elementPositionY = 26.596
enter image description here enter image description here
I try this code:
import arcpy
mxd = arcpy.mapping.MapDocument(r"G:\desktop\Project\project.mxd")
for elm in arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT"):
if elm.text == "data1":
elm.fontSize = 16
elm.elementPositionX = 9.898
elm.elementPositionY = 27.649
mxd.save()
del mxd
The code above works fine when i run for one map but when i loop it on several maps the code does not work:
import arcpy, os, sys
from arcpy import env
env.workspace = r"G:\desktop\Project"
for mxdname in arcpy.ListFiles("*.mxd"):
print mxdname
mxd = arcpy.mapping.MapDocument(r"G:\desktop\Project\\" + mxdname)
for elm in arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT"):
if elm.text == "data1":
elm.fontSize = 16
elm.elementPositionX = 9.898
elm.elementPositionY = 27.649
print 'elementPosition changed'
mxd.save()
del mxd
-
I'm confused by your title (which is meant to be a summary of your question) because it mentions "code error" while your question body provides no error message.PolyGeo– PolyGeo ♦2016年05月29日 08:23:45 +00:00Commented May 29, 2016 at 8:23
-
you right, i changed it now.newGIS– newGIS2016年05月29日 08:31:36 +00:00Commented May 29, 2016 at 8:31
-
Where is the "3" coming from that appears in your output? It does not appear to be from the code presented, and that code could easily be focussed down to a shorter snippet as recommended in both answers.PolyGeo– PolyGeo ♦2016年05月29日 09:20:53 +00:00Commented May 29, 2016 at 9:20
-
in the real code i use count variable- i removed it - please ignore itnewGIS– newGIS2016年05月29日 09:34:18 +00:00Commented May 29, 2016 at 9:34
-
1Have you tried setting the indentation level of the deletion of your MapDocument object to be the same as when you create it? You do that in the first code snippet but not when you start iterating. I think your code snippet could still benefit from more messaging while testing.PolyGeo– PolyGeo ♦2016年05月29日 12:34:36 +00:00Commented May 29, 2016 at 12:34
2 Answers 2
Your output suggests that your script found a layout element of type text element in each of three maps, and that it moved each of them.
When working with layout elements I think you would be wise to give each of them a unique name on their Size & Position tab so that you can use their name
property (e.g. elm.name
) to access them.
I'm not clear from your question as to what you mean by "nothing happen", because I would expect from that output that, if it was run while the MXDs were closed, then the next time you opened them the movement should have happened.
I suspect that the code you ran is not quite what you presented because there seems to be an extra "3" printed at the end which is not accounted for.
I think it would be useful if you could focus your question on what happens within a single map rather than looking at an iteration of three maps.
A more minimal code snippet to test would be:
import arcpy
mxd = arcpy.mapping.MapDocument(r"G:\PROJECTS\antiquities__55-30__403.mxd")
for elm in arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT"):
if elm.text == "data1":
elm.fontSize = 16
elm.elementPositionX = 9.898
elm.elementPositionY = 27.649
print 'elementPosition changed'
mxd.save()
del mxd
-
i tried your code- nothing changednewGIS– newGIS2016年05月29日 08:34:33 +00:00Commented May 29, 2016 at 8:34
-
1I agree - set a element name and use
elm.name
2016年05月29日 08:49:18 +00:00Commented May 29, 2016 at 8:49
All three properties you are working with are Read/Write properties, so to test what is happening and whether things are changing I would focus on just one MXD (and once it's working set it to loop through all your MXDs), and print the fontSize
, elementPositionX
, elementPositionY
before and after each change, to see what it is doing and whether it is actually changing anything.
import arcpy
mxd = arcpy.mapping.MapDocument(r"G:\PROJECTS\\antiquities__55-30__403.mxd")
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
if elm.text == "data1":
print "fontSize = {}".format(elm.fontSize) # return fontSize before change
elm.fontSize = 16
print "fontSize = {}".format(elm.fontSize) # return fontSize after change
print "elementPositionX = {}".format(elm.elementPositionX) # return elementPositionX before change
elm.elementPositionX = 9.898
print "elementPositionX = {}".format(elm.elementPositionX) # return elementPositionX after change
print "elementPositionY = {}".format(elm.elementPositionY) # return elementPositionY before change
elm.elementPositionY = 27.649
print "elementPositionY = {}".format(elm.elementPositionY) # return elementPositionY after change
mxd.save()
del mxd
-
i try this code- nothing changed in the mapnewGIS– newGIS2016年05月29日 09:06:50 +00:00Commented May 29, 2016 at 9:06
-
1but what did it print before and after each change? And are you trying to run this on an open map, or a closed one?2016年05月29日 09:07:37 +00:00Commented May 29, 2016 at 9:07
-
in printed: >>> fontSize = 21.7905 fontSize = 16.0 elementPositionX = 8.7748 elementPositionX = 9.898 elementPositionY = 26.8438 elementPositionY = 27.649 >>>newGIS– newGIS2016年05月29日 09:35:54 +00:00Commented May 29, 2016 at 9:35
-
and actually nothing changed in the mapnewGIS– newGIS2016年05月29日 09:36:24 +00:00Commented May 29, 2016 at 9:36
-
1are you trying to run this on an open MXD? If so then you need to use
arcpy.mapping.MapDocument("CURRENT")
andarcpy.RefreshActiveView()
2016年05月29日 09:42:18 +00:00Commented May 29, 2016 at 9:42