Hello all,
I find myself in a situation where I have a script which goes through a mdx (or series of mdx's in a folder(s)) repairing broken links. It works fine from ArcMap as a script tool, but we are thinking it may be better to run it from outside ArcMap (as it can be rather time consuming) as a python script. I'm trying to run it using pyscripter and am getting an error early on. Here is the code:
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">import</SPAN> Engine2 <SPAN class="keyword token">as</SPAN> eng
<SPAN class="keyword token">import</SPAN> ConfigParser
config <SPAN class="operator token">=</SPAN> ConfigParser<SPAN class="punctuation token">.</SPAN>RawConfigParser<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
configpath <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'X:\Development\ChrisH\BrokenLinks\Config\BrokenLinks.config'</SPAN>
config<SPAN class="punctuation token">.</SPAN>read<SPAN class="punctuation token">(</SPAN>configpath<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">main</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
mxdToCheck <SPAN class="operator token">=</SPAN> config<SPAN class="punctuation token">.</SPAN>get<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'data'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'mxdToReview'</SPAN><SPAN class="punctuation token">)</SPAN>
planningDataPath <SPAN class="operator token">=</SPAN> config<SPAN class="punctuation token">.</SPAN>get<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'data'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'planningDataPath'</SPAN><SPAN class="punctuation token">)</SPAN>
gdbSearchPath <SPAN class="operator token">=</SPAN> config<SPAN class="punctuation token">.</SPAN>get<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'data'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'gdbSearchPath'</SPAN><SPAN class="punctuation token">)</SPAN>
mxdFolder <SPAN class="operator token">=</SPAN> config<SPAN class="punctuation token">.</SPAN>get<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'data'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'mxdFolder'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> len<SPAN class="punctuation token">(</SPAN>mxdToCheck<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">></SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">:</SPAN>
brokenList <SPAN class="operator token">=</SPAN> eng<SPAN class="punctuation token">.</SPAN>GetBrokenLinks1<SPAN class="punctuation token">(</SPAN>mxdToCheck<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">GetBrokenLinks1</SPAN><SPAN class="punctuation token">(</SPAN>mxdIn<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token">#Get a list of broken layers in the passed in mxd</SPAN>
mxd <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>MapDocument<SPAN class="punctuation token">(</SPAN>mxdIn<SPAN class="punctuation token">)</SPAN>
brknList <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ListBrokenDataSources<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN> brknList
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>The error occurs at line 21. Here is the error message:
Traceback (most recent call last):
File "X:\Development\ChrisH\BrokenLinks\Scripts\links.py", line 185, in <module>
if __name__ == '__main__': main()
File "X:\Development\ChrisH\BrokenLinks\Scripts\links.py", line 41, in main
brokenList = eng.GetBrokenLinks1(mxdToCheck)
File "X:\Development\ChrisH\BrokenLinks\Scripts\Engine2.py", line 33, in GetBrokenLinks1
brknList = arcpy.mapping.ListBrokenDataSources(mxd)
File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\utils.py", line 181, in fn_
return fn(*args, **kw)
File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\mapping.py", line 1465, in ListBrokenDataSources
result = mixins.MapDocumentMixin(map_document_or_layer).listBrokenDataSources()
File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\arcobjects\mixins.py", line 832, in listBrokenDataSources
broken_sources = [l for l in self.layers if not l._arc_object.valid]
File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\arcobjects\mixins.py", line 683, in layers
for frame in reversed(self.dataFrames):
File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\arcobjects\mixins.py", line 695, in dataFrames
return map(convertArcObjectToPythonObject, self.pageLayout.dataFrames)
AttributeError: 'NoneType' object has no attribute 'dataFrames'
Thanks everyone