We have several hundred mxd maps that contain standard annotation feature classes. Each map is of different municipalities and each annotation file includes municipal boundary labels. Depending on the map and its purpose the exact phrasing differs but the boundary labels always include the word “BOUNDARY” and the date of the boundary. In this case, 2014. We need to have a code that can change 2014 to 2015. I wrote the below code to do a partial replace on the text string and it works… sometimes.
On the same data, it will sometimes not work at all (no error message; just no change in the text either), sometimes work perfectly, but most often mangles the process. It changes the TextString in the attribute table but if you click on the text and look at the “Annnotation” tab in the editor attributes window, it still has the old text (while the “Attributes” tab has the updated text). It also displays the old text on the map (with the new text in the table) even if the active frame is updated, the changes and file are saved, and if the file is reopened later.
import os
import arcpy
from arcpy import env
ws = “top_level_folder_path”
ws = arcpy.GetParameterAsText(0)
env.workspace = ws
subws = “folder_path_for_mxds”
subws = arcpy.GetParameterAsText(1)
byear = “2014″
byear = arcpy.GetParameterAsText(2)
nbyear = “2015″
nbyear = arcpy.GetParameterAsText(3)
fieldname = “TextString”
for root, dirs, files in os.walk(subws, topdown=True):
for file in files:
try:
if file.endswith(“.mxd”):
fullpath = os.path.join(root, file)
mxd = arcpy.mapping.MapDocument(fullpath)
edit = arcpy.da.Editor(ws)
edit.startEditing(True, True)
edit.startOperation()
for lyr in arcpy.mapping.ListLayers(mxd):
if (lyr.isGroupLayer):
try:
desc = arcpy.Describe(lyr)
if desc.dataElement.dataType == “FeatureClass”:
if (desc.featureType in ["Annotation", ""]):
anfc = lyr.dataSource
delimitedfield = arcpy.AddFieldDelimiters(anfc, fieldname)
with arcpy.da.UpdateCursor(anfc, delimitedfield, “TextString LIKE ” + “‘%BOUNDARY%” + byear + “‘”) as cursor:
for row in cursor:
row[0] = row[0].replace(byear, nbyear)
cursor.updateRow(row)
except:
arcpy.AddMessage(“There are no annotation files that contain boundary labels with the year ” + byear + ” in ” + fullpath)
pass
edit.stopOperation()
edit.stopEditing(True)
del mxd
except:
arcpy.AddMessage(“WARNING: ” + fullpath + ” cannot be processed. Check to make sure the file is not open, in use, or contains an existing lock.”)
pass
If anyone has any ideas, I would be very grateful. This problem has been making me cross-eyed for days.