I have a script that lists all layers in an MXD and I have a script that lists all broken layers in an MXD. I want to list all layers, their name, source, description, and whether or not their source is broken. Can anyone help me with this?
import arcpy, os, fnmatch, csv #Create an empty list of ArcMap documents to process... mxd_list=["A.mxd", "B.mxd", "C.mxd"] for mxd in mxd_list: mxd = arcpy.mapping.MapDocument(mxd) mapPath = mxd.filePath fileName = os.path.basename(mapPath) layers = arcpy.mapping.ListLayers(mxd) filepath = "C:/Users/USERNAME/Desktop/"+ fileName[:-4]+".csv" writer = csv.writer(file(filepath, 'wb')) sourcelist = [] for layer in layers: if layer.supports("dataSource"): layerattributes = [layer.longName, layer.dataSource] #Write the attributes to the csv file... writer.writerow(layerattributes) writer.writerow(sourcelist) del writer
I figured out an easy fix: I create an array of the broken layers, then check to see if the layers in the MXD are in that array. I instruct my output to write a "yes" or "No" to determine if the layer is broken.
#Import necessary librariesimport arcpy, os, fnmatch, csv #Create an empty list of ArcMap documents to process. mxd_list=[r"\\...\XYZ.mxd","//computer/d$/ABC.mxd"]#Initiate a for loop to run through each MXD in the listfor mxd in mxd_list: #Define the MXD mxd = arcpy.mapping.MapDocument(mxd) #Get the file path from the MXD mapPath = mxd.filePath #Get the name of the MXD from the full path fileName = os.path.basename(mapPath) #Get a list of all layers in the given MXD layers = arcpy.mapping.ListLayers(mxd) #Define the output file path. Here the file is being saved to the desktop with the same name as the MXD. Change the file path to an accessible location. filepath = "C:/Users/USERNAME/Desktop/"+ fileName[:-4]+".csv" #Define the file to be written as a CSV. writer = csv.writer(file(filepath, 'wb')) #Identify all broken layers in the MXD brknList = arcpy.mapping.ListBrokenDataSources(mxd) #Initiate a for loop to run through each layer within the MXD for layer in layers: #Check to see if the layer supports the attribute "dataSource". Not all layers support this stored metadata value. if layer.supports("dataSource"): #Determine if the layer is broken or not by comparing it to the list of broken layers if layer in brknList: broken = "Yes" else: broken = "No" #Compile a list of layer attributes. This list can be modified based on the defined properties of the layer class in arcpy (http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-mapping/layer-class.htm). layerattributes = [layer.longName, layer.dataSource, broken] #Write the attributes to the csv file. writer.writerow(layerattributes) #Delete variables that will be recycled in the loop del writer
#Create an empty list of ArcMap documents to process...
mxd_list=[ ]
directory = r'#your filespace to find mxds in#'
for root, dirs, files in os.walk(directory):
for file in files:
if ".mxd" in file:
mxd_path = os.path.join(root, file)
mxd_list.append(mxd_path)
Hi,
Thanks for your response.