How to iterate through tables in an mxd and replace their workspace paths using 10.4 and Python 2.7

820
4
10-08-2018 02:19 PM
MatthewHowe
New Contributor III

I am creating a script that walks through folders and scans for mxds. I use a set to get unique data sources and copy the data to a new folder keeping the same data format. I would then like to access the map document and repath all layers and tables to the copied data. I'm struggling to get the tables repathing code to work. In the below example, I have a fGDB table in the mxd and have copied the fGDB that it sits in to a new folder. I would thus like to repath to the table in the copied fGDB.

import arcpy, os
mxdrep = arcpy.mapping.MapDocument("CURRENT")
dfsrep = arcpy.mapping.ListDataFrames(mxdrep)

ignoreStringList = ["Vendor", "vendor", ".sde"]
#Target folder containing the copied fGDB
dbFolder = os.path.join(projFolderPath, "Databases")
for dfrep in dfsrep:
tablesRep = arcpy.mapping.ListTableViews(mxdrep, "", dfrep)
for tableRep in tablesRep:
     tablesRep = arcpy.mapping.ListTableViews(mxdrep, "", dfrep)
     tabRepDataSrc = tableRep.dataSource
     tableRepView = arcpy.mapping.TableView(tabRepDataSrc)
     if not any(folders in str(tabRepDataSrc) for folders in ignoreStringList):
          tabDirPath = tableRep.workspacePath
          tabWsPath = os.path.dirname(tabDirPath)
          tableRepView.findAndReplaceWorkspacePath(str(tabWsPath), dbFolder)
          #arcpy.mapping.AddTableView(dfrep, tableRepView)
0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus

I think you missed the python parser when embedding your code

/blogs/dan_patterson/2016/08/14/script-formatting 

0 Kudos
MatthewHowe
New Contributor III

Fixed, thanks Dan.

The only way I can seem to change the table workspace paths is to list table views, then replace the path string for each view, then add the table to the mxd then save. This works fine but causes problems if the original tables are internally joined to other layers. It seems odd that you can't change the workspace path for an existing table in the map. That said, I'm pretty sure I'm doing something wrong somewhere. 

0 Kudos
XanderBakker
Esri Esteemed Contributor

According to the help: Updating and fixing data sources with arcpy.mapping—Help | ArcGIS Desktop is states 

findAndReplaceWorkspacePath or findAndReplaceWorkspacePaths allows you to substitute an entire or partial string for a layer or table's workspace path. It can't be used if the workspace type or dataset name has changed. It is ideal for scenarios where drive letters change, switch to UNC paths, update SDE connection file information, and so on.

 

I wonder how much your data location has changed. Have you tried using this at a MapDocument level. The help also states that only joins associated to raster layers will not update. So in theory it should work.

MatthewHowe
New Contributor III

Ok so it was because I was trying to create a TableView from a TableView. The corrected code reads:

import arcpy, os
mxdrep = arcpy.mapping.MapDocument("CURRENT")
dfsrep = arcpy.mapping.ListDataFrames(mxdrep)

#Target folder containing the copied fGDB
dbFolder = os.path.join(projFolderPath, "Databases")
for dfrep in dfsrep:
   tablesRep = arcpy.mapping.ListTableViews(mxdrep, "", dfrep)
   for tableRep in tablesRep:
      tabDirPath = tableRep.workspacePath
      #Excludes data sources containing list of sub strings
      if not ".sde" in tabDirPath:
         tabWsPath = os.path.dirname(tabDirPath)
         tableRep.findAndReplaceWorkspacePath(str(tabWsPath), dbFolder)
mxdrep.save()
del mxdrep
0 Kudos