需求
移除mxd中的空图层,同时删除对应文件夹中的矢量数据
图示:test.mxd——>output.mxd
技术实现
数据路径硬编码:
##路径硬编码
import arcpy
mxd = arcpy.mapping.MapDocument(r"F:\zhouliyun\test2.mxd")
for df in arcpy.mapping.ListDataFrames(mxd):
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
rowCount = arcpy.GetCount_management(lyr)
if int(str(rowCount)) == 0:
arcpy.mapping.RemoveLayer(df, lyr)
print("remove layer: " + lyr.name)
filename = lyr.name + ".shp"
arcpy.Delete_management(filename)
print("delete layer: " + lyr.name)
#output mxd,the result show in the output mxd
mxd.saveACopy(r"F:\zhouliyun\Project5.mxd")
del mxd
数据路径软编码:
##路径软编码
import arcpy
#get mxd directory
input = arcpy.GetParameterAsText(0)
mxd=arcpy.mapping.MapDocument(r'%s'%input)
for df in arcpy.mapping.ListDataFrames(mxd):
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
rowCount = arcpy.GetCount_management(lyr)
if int(str(rowCount)) == 0:
arcpy.mapping.RemoveLayer(df, lyr)
print("remove layer: " + lyr.name)
filename = lyr.name + ".shp"
arcpy.Delete_management(filename)
print("delete layer: " + lyr.name)
#output mxd,the result show in the output mxd
output=arcpy.GetParameterAsText(1)
mxd.saveACopy(r'%s'%output)
del mxd
制作脚本工具
参考资料:
https://blog.csdn.net/qq_35515661/article/details/80849105
https://blog.csdn.net/wusuopuBUPT/article/details/23678291
脚本工具和测试数据链接:https://pan.baidu.com/s/1c1a0nGjqK4o5uG8YSFVQZw 密码:scs2