Add a table to TOC through script (10.0)

1340
3
02-11-2014 12:37 PM
ElliottCarson
New Contributor III
Hi,

I'm trying to add a table to the table of contents from a arcpy script.

It works fine through the command dialog but when using the commands within a script nothing seems to happen.

The command "arcpy.MakeTableView_management(...)" works fine in the command line.

I can add a layer through the script using lines, but there's no option to add a table the same way.

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
theShape = r"C:\Test.gdb\Test_FC"
newlayer = arcpy.mapping.Layer(theShape)
arcpy.mapping.AddLayer(df, newlayer, "BOTTOM")
del mxd, df, newlayer

Any ideas or adive would be greatly appreciated.

Regards,
Elliott
Tags (2)
0 Kudos
3 Replies
JohnDye
Occasional Contributor III
So is 'Test_FC' a Table or a Feature Class?
You indicate Table:

I'm trying to add a table to the table of contents from a arcpy script.

But FC indicates it might be a Feature Class...

If it is actually a GDB Table, you should be using:
mxd = arcpy.mapping.MapDocument("Current")
df = arcpy.mapping.ListDataFrames(mxd)[0]
TableView = arcpy.mapping.TableView(r'C:\Documents and Settings\jdk588\My Documents\ArcGIS\Default.gdb\MyTable')
arcpy.mapping.AddTableView(df, TableView)


You'll then need to manually switch the Table of Contents from the default 'List Layers by Drawing Order' view to the 'List By Source' view which is the only view under which you can see the table in the Table of Contents window.

I had hoped the 'arcpy.RefreshTOC()' function would automatically change the view based on the dataType of the last item added, but I found it doesn't and I don't know of any method in arcpy to change the TOC view, so you're probably stuck with the manual change now. Though, you could add a messagebox from the pythonaddins module to let you know the table was added.

mxd = arcpy.mapping.MapDocument("Current")
df = arcpy.mapping.ListDataFrames(mxd)[0]
try:
    TableView = arcpy.mapping.TableView(r'C:\Documents and Settings\jdk588\My Documents\ArcGIS\Default.gdb\MyTable')
    arcpy.mapping.AddTableView(df, TableView)
    pythonaddins.MessageBox("Table Added to TOC. Switch TOC to 'List By Source' view to access Table.", "Table Added", 0)
except Exception as e:
    pythonaddins.Messagebox("An error occured: " + e.message, "ERROR", 0)
0 Kudos
ElliottCarson
New Contributor III
Thanks for the reply John,

Unfortunately the commands you suggested are not available in 10.0.

I did end up finding the solution though. The script below requires an output to be defined in the parameters (see attachment).

import arcpy
tbl = arcpy.CreateTable_management("in_memory", "table")
arcpy.AddField_management(tbl, "F1", "TEXT", field_length=14)
arcpy.AddField_management(tbl, "F2", "TEXT", field_length=25)
arcpy.AddField_management(tbl, "F3", "TEXT", field_length=25)
arcpy.AddField_management(tbl, "F4", "TEXT", field_length=25)
arcpy.SetParameterAsText(0,arcpy.MakeTableView_management(table, "view"))
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
del tbl

Regards,
Elliott
0 Kudos
JohnDye
Occasional Contributor III

Unfortunately the commands you suggested are not available in 10.0.


Sorry Elliot, I didn't notice the 10.0 in parenthesis in your title. In that case, I would just use the MakeQueryTable tool, but omit any type of query. Kind of a workaround but a pretty elegant solution that could even be condensed to a single line if you just insert the path directly into the function instead of referring to a variable like I did. You don't even need to specify the MXD or DF with this tool and it will automatically switch your TOC view over to 'List by Source' once the Table is added.

theTable = r'C:\Documents and Settings\jdk588\My Documents\ArcGIS\Default.gdb\MyTable'
arcpy.MakeQueryTable_management(theTable, "TableNameinTOC", "NO_KEY_FIELD")


Only limitation here is that QueryTables are in-memory, so they won't persist after the session has ended. So if you run calculations or manipulate the table in any way, you'll need to write the result out again using 'TableToTable_conversion()' in order to keep it.
0 Kudos