I am using Python 2.7 and ArcGIS 10.4.1 to create 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)
tabRepDataSrc = tableRep.dataSource
#Excludes data sources containing list of sub strings
if not any(folders in tabRepDataSrc for folders in ignoreStringList):
#Create the table view for each table
tableRepView = arcpy.mapping.TableView(tabRepDataSrc)
# Get the directory level to replace
tabPath = os.path.dirname(tabRepDataSrc)
tabRepPath = os.path.dirname(tabPath)
#Repath the tables
tableRepView.findAndReplaceWorkspacePath(tabRepPath, dbFolder)
This gives a rather cryptic error;
Runtime error
Traceback (most recent call last):
File "<string>", line 9, in <module>
File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\utils.py", line 182, in fn_
return fn(*args, **kw)
File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\_mapping.py", line 1284, in findAndReplaceWorkspacePath
return convertArcObjectToPythonObject(self._arc_object.findAndReplaceWorkspacePath(*gp_fixargs((find_workspace_path, replace_workspace_path, validate), True)))
ValueError: Layer: Unexpected error
2 Answers 2
I found out I was actually creating a table view of a table view. 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
I have both a C# compiled version and original code of a couple Addin for Arc Catalog that do this task . in a zipped format but where do I host it for you. (newbie life)
-
preview.tinyurl.com/y7ujgpgaBigMat– BigMat2018年10月09日 01:44:20 +00:00Commented Oct 9, 2018 at 1:44
-
Thanks Mat. I need to embed it as part of a larger copy script so just after some help with the code for replacing the table paths. I can’t seem to find any examples even on the Esri pages.Howeitzer– Howeitzer2018年10月09日 06:18:30 +00:00Commented Oct 9, 2018 at 6:18
-
Just a thought. I dont know enough about coding , but I would have thought you could call the C# addin app from within the script if it was exposed in such a way that python could find and run it. albeit the addin requires some user input too. I cant recall where the original code came from but I am sure parts of it was on the edn at one stage.BigMat– BigMat2018年10月10日 19:20:21 +00:00Commented Oct 10, 2018 at 19:20