I know this is an old thread, but here is some code I wrote to do the trick. It can be incorporated into a loop for multiple MXDs.
<SPAN class="keyword token">import</SPAN> os<SPAN class="punctuation token">,</SPAN> sys<SPAN class="punctuation token">,</SPAN> arcpy<SPAN class="punctuation token">,</SPAN> re mxdFile <SPAN class="operator token">=</SPAN> <SPAN class="string token">"C:\\temp\\map_document.mxd"</SPAN> mxd101 <SPAN class="operator token">=</SPAN> <SPAN class="string token">'10.1'</SPAN> <SPAN class="operator token">+</SPAN> u<SPAN class="string token">'\u0013'</SPAN> mxd102 <SPAN class="operator token">=</SPAN> <SPAN class="string token">'10.1'</SPAN> <SPAN class="operator token">+</SPAN> u<SPAN class="string token">'\u0013'</SPAN> mxd103 <SPAN class="operator token">=</SPAN> <SPAN class="string token">'10.3'</SPAN> <SPAN class="operator token">+</SPAN> u<SPAN class="string token">'\u0014'</SPAN> mxd104 <SPAN class="operator token">=</SPAN> <SPAN class="string token">'10.4'</SPAN> <SPAN class="operator token">+</SPAN> u<SPAN class="string token">'\u0015'</SPAN> mxd105 <SPAN class="operator token">=</SPAN> <SPAN class="string token">'10.5'</SPAN> <SPAN class="operator token">+</SPAN> u<SPAN class="string token">'\u0016'</SPAN> mxd106 <SPAN class="operator token">=</SPAN> <SPAN class="string token">'10.6'</SPAN> <SPAN class="operator token">+</SPAN> u<SPAN class="string token">'\u0017'</SPAN> mxdList <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>mxd101<SPAN class="punctuation token">,</SPAN> mxd102<SPAN class="punctuation token">,</SPAN> mxd103<SPAN class="punctuation token">,</SPAN> mxd104<SPAN class="punctuation token">,</SPAN> mxd105<SPAN class="punctuation token">,</SPAN> mxd106<SPAN class="punctuation token">]</SPAN> result <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">with</SPAN> open<SPAN class="punctuation token">(</SPAN>mxdFile<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'rb'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> mxd<SPAN class="punctuation token">:</SPAN> fileContents <SPAN class="operator token">=</SPAN> mxd<SPAN class="punctuation token">.</SPAN>read<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>decode<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'latin1'</SPAN><SPAN class="punctuation token">)</SPAN> removedChars <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>x <SPAN class="keyword token">for</SPAN> x <SPAN class="keyword token">in</SPAN> fileContents <SPAN class="keyword token">if</SPAN> x <SPAN class="operator token">not</SPAN> <SPAN class="keyword token">in</SPAN> <SPAN class="punctuation token">[</SPAN>u<SPAN class="string token">'\xff'</SPAN><SPAN class="punctuation token">,</SPAN>u<SPAN class="string token">'\x00'</SPAN><SPAN class="punctuation token">,</SPAN>u<SPAN class="string token">'\x01'</SPAN><SPAN class="punctuation token">,</SPAN>u<SPAN class="string token">'\t'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN> joinedChars <SPAN class="operator token">=</SPAN> <SPAN class="string token">''</SPAN><SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>removedChars<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> m <SPAN class="keyword token">in</SPAN> mxdList<SPAN class="punctuation token">:</SPAN> location <SPAN class="operator token">=</SPAN> joinedChars<SPAN class="punctuation token">.</SPAN>find<SPAN class="punctuation token">(</SPAN>m<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> len<SPAN class="punctuation token">(</SPAN>joinedChars<SPAN class="punctuation token">[</SPAN>location<SPAN class="punctuation token">:</SPAN>location<SPAN class="operator token">+</SPAN><SPAN class="number token">4</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">></SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">:</SPAN> result<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>joinedChars<SPAN class="punctuation token">[</SPAN>location<SPAN class="punctuation token">:</SPAN>location<SPAN class="operator token">+</SPAN><SPAN class="number token">4</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> result<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><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>
>>> mxd.saveACopy("c:/output/base_93.mxd", "9.3")
Thanks but I know that already. I need to save the MXD out to the same version of ArcGIS. So if it's 9.3 I want to save to 9.3 but if it is 9.2 it needs to be saved as 9.2. I notice that if you leave out the version keyword it upgrades the MXD to 10.0.
No, not possible in Python in ArcGIS 10 to tell what release a mapdocument is currently saved as (I'm told possibly at 10.1)-Dave
Dave, Assuming you're still out there do you know if this got added to 10.1? I really need that able in a script I'm writing that strips out broken map service datasources. Thanks
Alas, no. I did see this strategy listed under the answer section that seems to hold though: http://gis.stackexchange.com/questions/62090/arcpy-method-to-determine-arcmap-document-version
Hi William - thanks for keeping the thread alive!
I'm trying to get your code to work in the Python window in Arc, but (I think) the indenting is screwing me up. I keep getting "IndexError: list index is out of range" for the last line, which I take to mean that the result list is empty.
Is there a way to share the python code with the indent formatting in tact, like used to be possible on GeoNet?
FYI: I'm using ArcMap 10.7.1 and trying the code on an MXD (I believe) I saved down to 10.3.
William's code was messed up, the indenting that is, in the transition from the old GeoNet hosting platform to the new hosting platform. It happened to a lot of code. Below is William's code reformatted. Just note that MXDs with versions newer than 10.6 will likely cause code to fail unless you update the code to include newer versions.
Although fixing indentation is a nuisance, being able to do it successfully is a critical step in learning and understanding Python.
import os, sys, arcpy, re mxdFile = "C:\\temp\\map_document.mxd" mxd101 = '10.1' + u'\u0013' mxd102 = '10.1' + u'\u0013' mxd103 = '10.3' + u'\u0014' mxd104 = '10.4' + u'\u0015' mxd105 = '10.5' + u'\u0016' mxd106 = '10.6' + u'\u0017' mxdList = [mxd101, mxd102, mxd103, mxd104, mxd105, mxd106] result = [] with open(mxdFile, 'rb') as mxd: fileContents = mxd.read().decode('latin1') removedChars = [x for x in fileContents if x not in [u'\xff',u'\x00',u'\x01',u'\t']] joinedChars = ''.join(removedChars) for m in mxdList: location = joinedChars.find(m) if len(joinedChars[location:location+4]) > 0: result.append(joinedChars[location:location+4]) print result[0]
Thanks for the quick fix, Joshua! That's the same indenting I ended up with, I was just doubting myself when I wasn't getting the results I expected.
I was running this on an MXD I thought I saved down to 10.3, using the saveACopy function from ArcPy. I was hoping to use William's code to verify it worked, but the result list is empty for any of the MXDs I save down with my custom Python Tool. However, I just tested William's code on an MXD I saved a copy of at 10.3 using the ArcMap 10.7.1 GUI, and his code did work, returning "10.3". So it looks like my saveACopy Python tool, based on the code here, is either not working properly, or the binary structure of the saved MXD's fileContents no longer matches the format this code is intended to interpret.
FYI: I've also added:
mxd107 = '10.7' + u'\u0018'
to the declared variables and added mxd107 to the mxdList, and can confirm that this will work on MXDs saved at 10.7.
Can we assume that mxd108 = '10.8' + u'\u0019' ?
Přihlášení členové mohou přispívat, sledovat aktualizace a další. Jste tu noví? Zaregistrujte si bezplatný účet.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.