代码:
#coding=gbk#author:杨勇#qq:1874445373#'''此脚本会报错,但是会正确的执行添加图层的操作。#可以添加.lyr .shp geodatabase唯独不能添加.dwgimport arcpy.mapping as mapping,os,arcpy,random,stringmxd_dir = arcpy.GetParameterAsText(0)#MXD目录add_layer = arcpy.GetParameterAsText(1)#.lyr目录;eg:c\\yy\\yy.lyr ;cad数据通过另存为lyr进行批量加载word = 'new_mxd_y'goal_path = os.path.join(mxd_dir,word)x = random.randint(0,10000)y = str(x)goal_path_new = os.path.join(goal_path,y)isExists = os.path.exists(goal_path)if not isExists:os.mkdir(goal_path)else:os.mkdir(goal_path_new)for root,dirs,files in os.walk(r"%s"%mxd_dir):for name in files:if name[-4:].lower() == '.mxd':filename = os.path.join(root,name)mxd =arcpy.mapping.MapDocument(r"%s"%filename)df = arcpy.mapping.ListDataFrames(mxd)[0]alayer = arcpy.mapping.Layer(r"%s"%add_layer)mapping.AddLayer(df,alayer,"AUTO_ARRANGE")if not isExists:mxd.saveACopy(os.path.join(goal_path,name))else:mxd.saveACopy(os.path.join(goal_path_new,name))