What layers are in an mxd

542 views
Skip to first unread message

Lew

unread,
Feb 21, 2013, 3:01:01 PM2/21/13
to Python - ARCGIS geoprocessing
Hi Pythoneers,

Does anyone have a script that lists the layer names in an mxd to a
txt, csv or xls?

Thanks
Lew

Lew.Gr...@gov.bc.ca

unread,
Feb 21, 2013, 6:41:31 PM2/21/13
to geop...@googlegroups.com, lew.gr...@gov.bc.ca
Here is a script that lists the layers in an mxd.
 
from arcpy import mapping
mapDoc = r"W:\FOR\RCO\DCR\Projects\Status\DCR_Status_Feb5_2013.mxd"
mxd = mapping.MapDocument(mapDoc)
lyrs = mapping.ListLayers(mxd)
lyrTitle = 'Map Layer'
sourceTitle = 'Data Source'
#print('%-60s%s' % (lyrTitle,sourceTitle))
#print('%-60s%s' % ('-' * len(lyrTitle), '-' * len(sourceTitle)))
for lyr in lyrs:
    try:
        #print('%-60s%s' % (lyr.name,lyr.dataSource))
        print('***%s' % (lyr.name))
        print('******%s' % (lyr.dataSource))
        print "                       "
    except:
        print('Unable to retrieve layer information')
del mxd

Will

unread,
Feb 22, 2013, 2:01:48 PM2/22/13
to geop...@googlegroups.com, lew.gr...@gov.bc.ca
Hey Lew -  here is a script I had that crawls through mxd's and outputs the layers and their respective mxds.
~Will

import os,sys,arcpy

mxdList = []
searchPath = r"W:\tsa\kam\alpine_ski\big_white\mxds"
outputTxt = r"w:\tsa\kam\alpine_ski\big_white\mxdCrawler_Output.csv"
#list mxds in search Path
for root,subFolders,files in os.walk(searchPath):
    for file in files:
        if os.path.splitext(file)[1] == '.mxd':
            mxdList.append(os.path.join(root,file))
#open file to write datasources to
if os.path.exists(outputTxt):
    print 'output exists.. exiting script'
    sys.exit(0)
else:
    f = open(outputTxt,'w')
#end    


for mapFile in mxdList:
    mxd = arcpy.mapping.MapDocument(mapFile)
    for lyr in arcpy.mapping.ListLayers(mxd):
        if lyr.supports("DATASOURCE"):
            txtStr = mapFile + ',' + lyr.dataSource + '\n'
            f.write(txtStr)

f.close()
Reply all
Reply to author
Forward
0 new messages