Thursday 22 November 2018

arcgis desktop - Changing second line text using ArcPy?


I work with several maps with this text:


enter image description here


enter image description here


and i try to change the second line only into "landUse" by using this code:



import arcpy
from arcpy import env

env.workspace = r"G:\desktop\Project"
for num, mxdname in enumerate((arcpy.ListFiles("*.mxd")),start = 1):
print '\n',num,mxdname
mxd = arcpy.mapping.MapDocument(
r"G:\desktop\Project\\" + mxdname)
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
elm.text = elm.text.replace(u'aaa'[1], u'landUse')

mxd.save()
del mxd

when i run it i get this unwanted result:


enter image description here


whereas i want this result:


enter image description here


i didn't find an answer in Changing part of text element using ArcPy?



Answer



Try replacing both the newline and 'aaa':



.replace('\naaa', "\nTEXT_ELEMENT")

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...