1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Iterate through tables in a map document and replace workspace paths using Python 2.7 and...

Discussion in 'Geography' started by Howeitzer, Oct 8, 2018.

  1. Howeitzer

    Howeitzer Guest

    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)
    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

    Login To add answer/comment

Share This Page