需求
移除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