Using ArcPy to determine ArcMap document version?
Using ArcPy to determine ArcMap document version?
23
I know this question is a few months old, but I'm posting this in case it helps others. I developed this kludge to parse version numbers from MXD documents. It basically reads the first 4000 or so characters of an MXD document and searches for a version number. I tested with MXD versions 9.2, 9.3, 10.0, and 10.1.
import re
def getMXDVersion(mxdFile):
matchPattern = re.compile("9.2|9.3|10.0|10.1|10.2")
with open(mxdFile, 'rb') as mxd:
fileContents = mxd.read().decode('latin1')[1000:4500]
removedChars = [x for x in fileContents if x not in [u'\xff',u'\x00',u'\x01',u'\t']]
joinedChars = ''.join(removedChars)
regexMatch = re.findall(matchPattern, joinedChars)
if len(regexMatch) > 0:
version = regexMatch[0]
return version
else:
return 'version could not be determined for ' + mxdFile
Here is an example of scanning a folder for mxd files and printing the version and names
import os
import glob
folder = r'C:\Users\Administrator\Desktop\mxd_examples'
mxdFiles = glob.glob(os.path.join(folder, '*.mxd'))
for mxdFile in mxdFiles:
fileName = os.path.basename(mxdFile)
version = getMXDVersion(mxdFile)
print version, fileName
Which returns this:
>>>
10.0 Arch_Cape_DRG.mxd
9.2 class_exercise.mxd
9.3 colored_relief2.mxd
10.1 CountyIcons.mxd
10.0 DEM_Template.mxd
9.2 ex_2.mxd
10.0 nairobimap.mxd
10.0 slope_script_example.mxd
10.1 TrailMapTemplateBetter.mxd
10.0 Wickiup_Mountain_DEM.mxd
>>>
The function below is based on Ryan's idea, but is a little more direct. ArcGIS map documents are actually OLE documents, which can be parsed with the oletools module (available on pypi: https://pypi.python.org/pypi/oletools). The function opens the file and reads the version string. Tested with 9.0, 9.3, 10.1 and 10.3, but should work with anything (not sure about 3.x...).
from oletools.thirdparty import olefile
def mxd_version(filename):
ofile = olefile.OleFileIO(filename)
stream = ofile.openstream('Version')
data = stream.read().decode('utf-16')
version = data.split('\x00')[1]
return version
if __name__ == '__main__':
import sys
print(mxd_version(sys.argv[-1]))
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2019-04-18 GIS 案例教程-蜂窝多边形制作模型