Summary
Provides access to basic table properties.
Discussion
The TableView object is essential for managing stand-alone tables that reside within a map document (.mxd). It provides access to basic table properties such as data source information and setting a table's definition query. In addition to the TableView constructor function, the ListTableViews function also provides the ability to reference a TableView object.
The TableView constructor function allows you to reference a table outside of a map document within a workspace. This provides the ability to add an external table into a map document using the AddTableView function. This function is similar to MakeTableView, but the difference is that the result from MakeTableView can't be added permanently into a map document.
The ListTableViews function returns a Python list of TableView objects. It is necessary to then iterate through each item in the list or specify an index number to reference a specific TableView object. Tables can be searched within an entire map document or within a specific data frame. Wildcards can also be used to limit the search.
A definition query will not work for all workspaces. It is important to use the correct SQL syntax when working with different workspaces. For example, file geodatabases and shapefiles have double quotes around a field name (for example, "field_name"), personal geodatabases have brackets (for example, [field_name]), and SDE connections don't have any special characters (for example, field_name). For more information on updating workspaces and data sources in a map document or layer file, please refer to the Updating and Fixing Data Sources with arcpy.mapping help topic.
Tables can be removed from a map document using the RemoveTableView function.
Syntax
TableView (table_view_data_source)
Parameter | Explanation | Data Type |
table_view_data_source | A string that includes the full workspace path, including the name of the table. | String |
Properties
Property | Explanation | Data Type |
datasetName (Read Only) | Returns the name of the table's dataset the way it appears in the workspace, not in the table of contents. | String |
dataSource (Read Only) | Returns table's data source path. It includes the workspacePath and the datasetName combined. | String |
definitionQuery (Read and Write) | Provides the ability to get or set a tables's definition query. | String |
name (Read and Write) | Provides the ability to set or get the name of a table the way it would appear in the ArcMap table of contents. Spaces can be included. | String |
isBroken (Read Only) | Returns True if a table's data source is broken. | Boolean |
workspacePath (Read Only) | Returns a path to the table's workspace or connection file. | String |
Method Overview
Method | Explanation |
findAndReplaceWorkspacePath (find_workspace_path, replace_workspace_path, {validate}) | Replaces a table's workspace with a new workspace path |
getSelectionSet () | Returns a table's selection as a Python set of Object IDs. |
replaceDataSource (workspace_path, workspace_type, {dataset_name}, {validate}) | Replaces a table's data source in a map document (.mxd); also provides the ability to switch workspace types (for example, replace a file geodatabase workspace with an SDE workspace). |
setSelectionSet (method, oidList) | Sets a table's selection using a Python set of Object IDs. |
Methods
findAndReplaceWorkspacePath (find_workspace_path, replace_workspace_path, {validate})
Parameter | Explanation | Data Type |
find_workspace_path | A string that represents the workspace path or connection file you want to find. If an empty string is passed, then all workspace paths will be replaced with the replace_workspace_path parameter depending on the value of the validate parameter. | String |
replace_workspace_path | A string that represents the workspace path or connection file you want to use to replace. | String |
validate | If set to True, the workspace will only be updated if the replace_workspace_path value is a valid workspace. If it is not valid, the workspace will not be replaced. If set to False, the method will set the workspace to match the replace_workspace_path, regardless of a valid match. In this case, if a match does not exist, then the table's data source would be broken. (The default value is True) | Boolean |
For more detailed discussion, parameter information, scenarios, and code samples, please refer to the Updating and fixing data sources help topic.
getSelectionSet ()
Return Value
Data Type | Explanation |
List | Returns a table's selection as a Python set of Object IDs. |
Provides an easy way to retrieve the table's current selection.
replaceDataSource (workspace_path, workspace_type, {dataset_name}, {validate})
Parameter | Explanation | Data Type |
workspace_path | A string that includes the workspace path to the new data or connection file. | String |
workspace_type | A string keyword that represents the workspace type of the new data.
| String |
dataset_name | A string that represents the name of the table the way it appears in the new workspace (not the name of the table in the table of contents). If dataset_name is not provided, the replaceDataSource method will attempt to replace the dataset by finding a table with a the same name as the layer's current dataset property. | String |
validate | If set to True, a workspace will only be updated if the workspace_path value is a valid workspace. If it is not valid, the workspace will not be replaced. If set to False, the method will set the workspace to match the workspace_path, regardless of a valid match. In this case, if a match does not exist, then the data source would be broken. (The default value is True) | Boolean |
For more detailed discussion, parameter information, scenarios, and code samples, please refer to the Updating and Fixing Data Sources help topic.
setSelectionSet (method, oidList)
Parameter | Explanation | Data Type |
method | A string that specifies which selection method to use.
(The default value is None) | String |
oidList | A Python set of object IDs to use along with the appropriate selection method. | Integer |
This method provides an easy way to manage a table's selection. To clear the selection, use the NEW selection method with an empty set. Python Sets or Lists can be used for the oidList, but sets get returned from the getSelectionSet method on the Table object.
Code sample
TableView example 1
The following script will find a table called Customers in a data frame named Transportation and will set its definition query.
import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Transportation")[0]
for table in arcpy.mapping.ListTableViews(mxd, "", df):
if table.name.lower() == "trafficaccidents":
table.definitionQuery = "\"age\" >= 18"
mxd.save()
del mxd
TableView example 2
The following script will reference a table in a file geodatabase and then add that table into the referenced map document.
import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Transportation")[0]
accidentsTable = arcpy.mapping.TableView(r"C:\Project\Data\Transportation.gdb\Accidents")
arcpy.mapping.AddTableView(df, accidentsTable)
mxd.saveACopy(r"C:\Project\Project2.mxd")
del mxd, accidentsTable