import os, arcpy, arcpy.mapping oldText = arcpy.GetParameterAsText(0) newText = arcpy.GetParameterAsText(1) pdf = arcpy.GetParameterAsText(2) mxd=arcpy.mapping.MapDocument("Current") # Loop through each text element in the map document for textElement in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"): # Check if the text element contains the out of date text if textElement.text == oldText: # If out of date text is found, replace it with the new text textElement.text = '<FNT name="Arial">' + newText + '</FNT>' #while PDF = 1: # Export the updated map to a PDF #path = r'P:\2011\Job_031_TownPlanning_SeriesProduction\Working\draft_output\esri\2\wetland\' #arcpy.mapping.ExportToPDF(mxd, newText + "-TownPlanning_Wetlands_Overlay_Ver2.pdf") #arcpy.mapping.ExportToJPEG(mxd, outFile) #outFile = newText + df.name + ".pdf" # Clean up the MapDocument object by deleting it del mxd arcpy.RefreshActiveView() arcpy.RefreshTOC()
import os, arcpy, arcpy.mapping oldText = arcpy.GetParameterAsText(0) newText = arcpy.GetParameterAsText(1) PDF = arcpy.GetParameterAsText(2) mxd=arcpy.mapping.MapDocument("Current") OldFont='<FNT name="Vivaldi"' NewFont='<FNT name="Arial"' # Loop through each text element in the map document for textElement in arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT"): #Check if the text element contains the out of date text if str(oldText) in str(textElement.text): #If out of date text is found, replace it with the new text Text=str (textElement.text) Text=Text.replace(OldFont,NewFont) Text=Text.replace(oldText,newText) textElement.text=str(Text) if PDF == "1": #Export the updated map to a PDF path = 'C:\Project' arcpy.mapping.ExportToPDF(mxd, path + "/TownPlanning_Wetlands_Overlay_Ver2.pdf") #Clean up the MapDocument object by deleting it del mxd arcpy.RefreshActiveView() arcpy.RefreshTOC()
>>> mxd_r=r'P:\2011\Job_031_TownPlanning_SeriesProduction\Working\mxd\B&L\2\1-7_TownPlanning_B&L.mxd'
>>> mxd_current=arcpy.mapping.MapDocument("Current")
>>> mxd_root=arcpy.mapping.MapDocument(mxdr)
>>> Textelm_root=arcpy.mapping.ListLayoutElements(mxd_root,"TEXT_ELEMENT")
>>> Textelm_current=arcpy.mapping.ListLayoutElements(mxd_current,"TEXT_ELEMENT")
>>> Textelm_current
[<TextElement object at 0x203f1430[0x203b6fa0]>, <TextElement object at 0x203f12f0[0x203b6d40]>, <TextElement object at 0x203f1450[0x203f1380]>, <TextElement object at 0x203a5310[0x203f13a0]>, <TextElement object at 0x203f14b0[0x203f1260]>, <TextElement object at 0x203f14f0[0x203f1060]>, <TextElement object at 0x203f1510[0x203f13c0]>]
>>> Textelm_root
[<TextElement object at 0x203f1250[0x203f10e0]>, <TextElement object at 0x203a5090[0x203f11c0]>, <TextElement object at 0x203f1050[0x203f10c0]>, <TextElement object at 0x203f1150[0x203f1180]>, <TextElement object at 0x203f12b0[0x203f11a0]>, <TextElement object at 0x203f12d0[0x203f11e0]>]
>>> Textelm_current[1]=Textelm_root[1]
>>> arcpy.RefreshActiveView()
mxd=arcpy.mapping.MapDocument("Current") # Loop through each text element in the map document for textElement in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"): # Finds the right textelement if textElement.name == "Text_Element1": #Replaces the old text by new textElement.text = '<FNT name="Arial" size = "10">' + "My New text" + '</FNT>' #plus whatever other HTML font style specification u did elif textElement.name == "Text_Element1": textElement.text = '<FNT name="Aria2" size = "12">' + "My New text2" + '</FNT>' elif textElement.name == "Text_Element3": textElement.text = '<FNT name="Arial" size = "12">' + "My New text3" + '</FNT>' #etc..... del mxd