操作方法
使用地图文档 (MXD) 时,由于数据更改或进行了修订,可能需要替换或更新文本元素。 Python 中的 text.Split() 函数可以用于同时替换多个 MXD 中的文本元素以节省时间,而不必单独更新每个 MXD 中的文本。
在 ArcMap 中使用 Python 脚本工具,创建两个文本列表以将旧的和新的文本元素存储在 MXD 中,并将与第一个列表匹配的任何文本元素更改为第二个列表中的相应文本。 为此,请执行以下步骤。
import arcpy
from arcpy import env
import os
arcpy.env.overwriteOutput = True
#set path to the relevant folder
arcpy.env.workspace = Input_Workspace = arcpy.GetParameterAsText(0)
Output_Workspace = arcpy.GetParameterAsText(1)
Old_Text = arcpy.GetParameterAsText(2)
oldList = Old_Text.split(', ') #set the comma as separator for multiple inputs
New_Text = arcpy.GetParameterAsText(3)
newList = New_Text.split(', ')
#list the mxds of the workspace folder
for mxdname in arcpy.ListFiles("*.mxd"):
arcpy.AddMessage(mxdname)
#set the variable
mxd = arcpy.mapping.MapDocument(Input_Workspace + "\\" + mxdname)
#replace elements that occur in the map document
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
counter = 0
for text in oldList:
if text in elm.text:
elm.text = elm.text.replace(text, newList[counter])
arcpy.AddMessage('{} changed'.format(elm.text))
counter = counter + 1
else:
counter = counter + 1
mxd.saveACopy(Output_Workspace + "\\" + mxdname)


注: 该脚本根据 MXD 的名称按字母顺序对其进行遍历。 更新文本元素时,从按字母顺序的第一个 MXD 开始进行,以便根据需要进行更改。
注: 在 Old_Text 字段中键入文本时,请确保要替换的文本元素与每个 MXD 中显示的完全一致。 否则,脚本无法识别文本,更改不会反映在输出 MXD 中。
文章 ID: 000015333
获取来自 ArcGIS 专家的帮助
立即开始聊天