Skip to content

Instantly share code, notes, and snippets.

@mu88
Created March 1, 2018 16:10
Show Gist options
  • Save mu88/94ff4b871e38db5378c27a28aaa58b30 to your computer and use it in GitHub Desktop.
Save mu88/94ff4b871e38db5378c27a28aaa58b30 to your computer and use it in GitHub Desktop.
ArcPy - Get Layer Data Sources of MXDs
import arcpy, codecs, csv, os
mxd_folder = "C:\\temp\\MXDs"
output = "C:\\temp\\dataSources.csv"
f = codecs.open(output, "w", encoding="utf-8")
f.write("MXD;Layer;Data Source\n")
for file in os.listdir(mxd_folder):
if file.endswith(".mxd"):
mxd = os.path.join(mxd_folder, file)
mxdFilename = os.path.splitext(file)[0]
mapDocument = arcpy.mapping.MapDocument(mxd)
for lyr in arcpy.mapping.ListLayers(mapDocument):
layerName = lyr.name
if lyr.supports("DATASOURCE"):
layerDataSource = lyr.dataSource
f.write(mxdFilename + "; " + layerName + "; " + layerDataSource + "\n")
else:
f.write(mxdFilename + "; " + layerName + "; <<COULD NOT READ DATA SOURCE>>\n")
f.close()
print "Completed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment