How to print the list of "table of contents (TOC)"

10761
10
10-14-2014 04:54 PM
YoshiTsuzuki
New Contributor II

Hello

I want to get a list of source files in the table of contents (TOC) with ArcGIS 10.2.

Source files are displayed in TOC with "List by source" mode. I want to get simply the list of source files. Moreover, source files of vector files are displayed in the TOC, but source files of raster files and tables (csv, xls, etc.) are not displayed in TOC, and only can found by checking the properties of the files. Please advise me if you know the methods to obtain the source file list of TOC.

Thanks in advance.

Yoshi

0 Kudos
10 Replies
ChristianWells
Esri Regular Contributor

One way that this could be found is by using the arcpy.mapping module to list out all the layers in your MXD. Here is an example of that where it will list both the fully qualified file path and the workspace path:

import arcpy

mxd = arcpy.mapping.MapDocument("<MXDPath>")

df = arcpy.mapping.ListDataFrames(mxd)

for d in df:

     print "Data Frame: " + d.name

     layers = arcpy.mapping.ListLayers(mxd, "", d)

     for lyr in layers:

          print "Layer Name: " + lyr.name

          print "Data Source: " + lyr.dataSource #This will return the fully qualified path of the file (C:\Shapefiles\femapnls.shp)

          print "Workspace Path: " + lyr.workspacePath #This will return the workspace path of the file (C:\Shapefiles)

Output:

Data Frame: Layers

Layer Name: FEMA

Data Source: C:\Shapefiles\femapnls.shp

Workspace Path: C:\Shapefiles

Note: This output will be reflect a similar path name if there are raster, csv, dbf, etc. in the MXD (C:\Raster\Aerial.tif)

0 Kudos
YoshiTsuzuki
New Contributor II

Hi Christian

Thank you for your reply. If that script works, for the MXD file with several source files will show multiple source files in the Output. I am a little new in Python script. I open python window: "Geoprocessing" - "Python", and copy your script lines by lines with one of MXD files on PC.

My question this time is where the Output is displayed. I check some windows, and could not find the Output.

I opened python window from the python.exe file directly and try to run the script in the window with "File  Edit Format Run Options Windows Help" menu, but could not find Output file with "Run" - "Run module". In this case, an error message is displayed in another window as the following, and this method does not seem to be correct. Correct method may be conducted in the Python window in ArcGIS as the above. If this is correct, my question is where the Output is found.

Traceback (most recent call last):

  File "C:/Python26/Scripts/print_source_file01.py", line 1, in <module>

    import arcpy

ImportError: No module named arcpy

This may be a basic question, and look for a reply.

Thanks in advance.

Yoshi

0 Kudos
ChristianWells
Esri Regular Contributor

Hi Yoshi, you are on the right track for running the python command in the MXD. If you open the Python window and then paste each line into the python window, you should see the following output. The only change that you would need to make is to set arcpy.mapping.MapDocument() to arcpy.mapping.MapDocument("CURRENT"). This will set the mxd in Python to match the current ArcMap session. The output results should print in the same window you are working in.

PythonTOC.png

If you want to run this outside of ArcMap, we can use a program called IDLE which is installed with ArcGIS.

1. To get to it, go to the Start Menu > All Programs > ArcGIS > Python 2.7 > IDLE (Python GUI). This should open a new Python shell window.

2. From this window, click File > New. Paste the entire code into this window.

3. Change the path of the MapDocument to match your MXD.

Example:

mxd = arcpy.mapping.MapDocument(r"C:\Temp\Python.mxd")

*make sure that a lowercase "r" is used before the path

4. Save the Python file

5. Click Run > Run Module

6. All the results will be printed on screen

IDLEResults.png

Both of the methods are acceptable for running this script. Just remember that in the script provide, you can delete any of the print statements that you don't want to see on the final output.

print "Data Frame: " + d.name

print "Layer Name: " + lyr.name 

print "Data Source: " + lyr.dataSource

print "Workspace Path: " + lyr.workspacePath

0 Kudos
YoshiTsuzuki
New Contributor II

Thank you Christian for your reply.

I have tried to follow your methods, however, still cannnot find the Output.

For the Python window in ArcGIS, Output could not displayed in the same window. Please check the image. I have applied a few methods into the parenthesis of "mxd = arcpy.mapping.MapDocument" line: ("CURRENT") and with/without r, ("C:\ArcGIS\example2.mxd") and (r"C:\ArcGIS\example2.mxd").

Capture1.PNG

On the contrary, for the Python IDLE program, Output was displayed in another window. I will use this methods for a while.

Capture3.PNG

I appreciate any further advice for improvement, including solutions for the Python window in ArcGIS.

0 Kudos
YoshiTsuzuki
New Contributor II

I have found an error message in the method with Python IDLE program as the following. When this message appears, some data sources are in an external drive which is not available at this time. Is it the reason for this error message?

============== Error message (from here) ==============================

Traceback (most recent call last):

  File "C:/Python27/ArcGIS10.2/Scripts/PrintSourceFiles3.py", line 10, in <module>

    print "Data Source: " + lyr.dataSource

  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\arcobjects\_base.py", line 78, in _get

    (attr_name, self.__class__.__name__))

NameError: The attribute 'dataSource' is not supported on this instance of Layer.

============== Error message (up to here) ==============================

0 Kudos
ChristianWells
Esri Regular Contributor

To show the output for the Python, can you try and run the the script but at the end, press enter two times to start the script.

0 Kudos
Zeke
by
Regular Contributor III

In the Python window in ArcMap, you will definitely need the r before "C:\ArcGIS\...". If you don't use r, you'll have to either use double back slashes - "C:\\ArcGIS\\..." or use forward slashes - "C:/ArcGIS/...". Python treats a single backslash in a string as an escape character, e.g. "\n" = newline, "\t" = tab, etc. Although in the Python window you're generally working with the mxd you have open, so "CURRENT" is correct there.

Not sure why you're failing on lyr.dataSource, unless there's a typo somewhere. dataSource is a valid layer property. Is your shapefile in a group layer by chance?

0 Kudos
YoshiTsuzuki
New Contributor II

Hi Greg

Thank you for your advice on th

0 Kudos
YoshiTsuzuki
New Contributor II

Hi Greg

Thank you for your reply for the format to designate mxd project files in detail and the error message. For the error message, the files are not existed when the error messages are appeared, they were existed when the mxd projects were developed.

Cheers

Yoshi

0 Kudos