Select to view content in your preferred language

Adding table to Table of Contents

2108
5
Jump to solution
08-05-2013 04:23 PM
TerrellBeesley
Emerging Contributor
I am using Arc 10.1. I am trying to add a table to the Table Of Contents using the following script but it doesn't show up without saving to a new map mxd document. I am using the following code:

                mxd = arcpy.mapping.MapDocument('CURRENT')
                df = arcpy.mapping.ListDataFrames(mxd, "layers")[0]
                accidentsTable = arcpy.mapping.TableView("H:/ss/Projects/MaPP/Habitat.gdb/tablename")
                arcpy.mapping.AddTableView(df, accidentsTable)

This seems to run fine but it's not showing up in the Table of Contents. I've tried using following:

                arcpy.RefreshTOC()
                arcpy.RefreshActiveView()
              
but it still doesn't show up. If I save the mxd file under a different name and reload the file using

                mxd.saveACopy(r"C:\Project\Project2.mxd")

the table is in the Table of Contents, but that kind of defeats the purpose.

It seems like I'm missing something simple. Does anybody have any ideas on how I can add a newly created data table to the active data frame and have it show up in the currently open map document.
Much thanks for any help on this.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
TimDonoyou
Regular Contributor
Hi there

this might be too obvious and apologies if so but did you set an 'table' output parameter when adding your script to toolbox?

thanks

Tim

View solution in original post

0 Kudos
5 Replies
ArkadiuszMatoszka
Frequent Contributor
Hi,
Are you running your script as toolbox, add in or from python window?
In last two cases running:
arcpy.MakeTableView_management("H:/ss/Projects/MaPP/Habitat.gdb/tablename", "TMP")

will add tabel under name of "TMP" to TOC of mxd in context of which you invoked command (select "List by source" tab in TOC).

Best Regards
Arek
SethSobel
Occasional Contributor

@ArkadiuszMatoszka wrote:
Hi,
Are you running your script as toolbox, add in or from python window?
In last two cases running:
arcpy.MakeTableView_management("H:/ss/Projects/MaPP/Habitat.gdb/tablename", "TMP")

will add tabel under name of "TMP" to TOC of mxd in context of which you invoked command (select "List by source" tab in TOC).

Best Regards
Arek

 

This solution is what worked for me. Using arcpy in ArcGIS Pro, I had several times where the output of my process (a table) was not getting added to my TOC. When the table is not added, the next step breaks with the error that the table does not exist or is not supported. It happens, seemingly at random, as I can run it again and it will work. Using this snippet to add the table explicitly adds a small step to the process, but seems to resolve my problem.

Thank you!

0 Kudos
TerrellBeesley
Emerging Contributor
Hi Arek
I am running the script from a toolbox in ArcMap. I tried your idea anyway but it still doesn't add the table to a data frame in the Table of Contents. I can add it manually through "Add Data". It then creates a new data frame and adds the table. I would like to do the same using a python script if possible.
Thanks Arek for your help.
0 Kudos
TimDonoyou
Regular Contributor
Hi there

this might be too obvious and apologies if so but did you set an 'table' output parameter when adding your script to toolbox?

thanks

Tim
0 Kudos
TerrellBeesley
Emerging Contributor
Thanks Tim, that worked.

[ATTACH=CONFIG]26523[/ATTACH]    

Thank you for the tip. I knew it would be something simple.
0 Kudos