Hi there,
i try to change oldText with myString in 25 mxd's ( in the layout ).
The text (i used draw tool to create it) contain 2 row:
land use
fuel
myString is the new text
i try this code but python only print list of mxd names in the folder:
import arcpy from arcpy import env env.workspace = r"D:\PROJECTS\ab\gis" counter = 0 for mxdname in arcpy.ListFiles("*.mxd"): print mxdname oldText = 'land use' oldText = oldText + '\n' oldText = oldText + 'fuel' mxd = arcpy.mapping.MapDocument(r"D:\PROJECTS\ab\gis\\" + mxdname) myString = 'free fuel' myString = myString + '\n' myString = myString + 'gas' for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"): if elm.text == oldText : print elm.name elm.text = myString mxd.save() counter = counter + 1 del mxd
is it possible with python 2.7.8?
thanks in advance.
Solved! Go to Solution.
thanks for everyone,
finally i use this code:
import arcpy from arcpy import env env.workspace = r"D:\PROJECTS\ab\gis" for mxdname in arcpy.ListFiles("*.mxd"): print mxdname mxd = arcpy.mapping.MapDocument(r"D:\PROJECTS\ab\gis\\" + mxdname) for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"): oldText = 'land use\nfuel' newText = u'free fuel\ngas' if elm.text == oldText: elm.text = newText print 'elm.text changed' mxd.save() del mxd
oldText = 'land use' + '\n' + 'fuel'
myString = 'free fuel + '\n' + 'gas'
show some output
it isn't a python issue
i get:
>>>
environment 1.mxd
environment 2.mxd
environment 3.mxd
til i get
environment 25.mxd
>>>
it only print mxd name
Print out the text elements without an if statement to ensure that your syntax for oldText is correct
do you actually print out oldtext? all you print out is
print mxdname
ergo you get
environment 1.mxd
environment 2.mxd
environment 3.mxd
til i get
environment 25.mxd
>>>
it only print mxd name
put in print statements if you want to test stuff
Basic python question here!, you just get the mxd names, as the if statement is never satisfied.
To resolve, try changing your code as follows, then you should be able to work out what has happened!
import arcpy from arcpy import env env.workspace = r"D:\PROJECTS\ab\gis" counter = 0 for mxdname in arcpy.ListFiles("*.mxd"): print mxdname oldText = 'land use' oldText = oldText + '\n' oldText = oldText + 'fuel' mxd = arcpy.mapping.MapDocument(r"D:\PROJECTS\ab\gis\\" + mxdname) myString = 'free fuel' myString = myString + '\n' myString = myString + 'gas' print "We are looking for a value of: " print oldText for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"): if elm.text == oldText: print elm.name elm.text = myString else: print "Non Matching Element value of:" print elm.text mxd.save() counter = counter + 1 del mxd
i try your code Luke and got result :
>>> environment 1.mxd We are looking for a value of: land use fuel Non Matching Element value of: land use fuel environment 2.mxd We are looking for a value of: land use fuel >>>
the oldText still remain the same in the layout :
there is a couple of possibilities:
To elaborate consider the following examples (not all combinations checked):
>>> a = "land" + "\nl" + "use" >>> a == a True >>> # now try some variations >>> b = "land " + "\nl" + "use" # extra space after 'land' >>> a == b False >>> c = "land" + "\nl" + "use " # extra space after 'use' >>> a == c False >>> d = "land" + "use" >>> a == d False
i will try-thanks