Try this. The line elm.Text won't work. Capitalization is important, as is indenting. Though in this case, the indentation you used was fine. for elm in arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT",element_name): elm.text = var_text
for elm in arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT",element_name): elm.text = var_text
so it works the first time but fails to change the text when you run the same code a second time:var_text = "1200" >>> element_name = "map_ID" >>> for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", element_name): ... if elm.name == element_name: ... elm.Text = (var_text) first time no problem at all, second time I just change var_text to some other thing "1201" for instance and it doesn't update the text. Any ideas?Edit: More info, if I double click on text and then exit properties, then run the script again with a different # for var_text it works. This defeats the purpose of the script though (automate the maps) so hoping to find a way around it
var_text = "1200" >>> element_name = "map_ID" >>> for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", element_name): ... if elm.name == element_name: ... elm.Text = (var_text)
#============================================ # Change Text Based Using Look Up Table #============================================ import arcpy import os mxds = [r"C:\My_Stuff\GIS\Text\Map1.mxd","C:\My_Stuff\GIS\Text\Map2.mxd"] #List of mxds table = [r"C:\My_Stuff\GIS\Text\DynamicText.gdb\LookupText"] # Lookup table for mxd in mxds: rows = arcpy.SearchCursor(table) mapp = arcpy.mapping.MapDocument(mxd) for row in rows: val = row.getValue("MapName") #Fieldname containing map names if val + ".mxd" == os.path.basename(mxd): txt = row.getValue("Text") #Fieldname containing text string txt_elms = arcpy.mapping.ListLayoutElements(mapp,"TEXT_ELEMENT") for elm in txt_elms: if elm.text == "<TextVariable>": #Text in all maps to be replaced elm.text = txt mapp.save() del rows, row
Right click on the text element and select properties. Go to the Size and Position tab. Set the Element Name there. Be sure all elements that you want to find have a unique name.Jeff
Here's what I do. var_text = "Some title/number etc" element_name = "ID" for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", element_name): if elm.name == element_name: elm.text = (var_text)
var_text = "Some title/number etc" element_name = "ID" for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", element_name): if elm.name == element_name: elm.text = (var_text)
Los miembros registrados pueden publicar, seguir actualizaciones y más. ¿Nuevo aquí? Regístrate gratis.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.