Create new Map Document (*.mxd) using ArcPy

4264
8
02-12-2013 03:24 PM
Status: Open
Labels (2)
GraemeBrowning
Regular Contributor
I know that arcpy.mapping was not designed to author new objects - http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s30000006s000000 - but I wish that it had been and would like to see ArcPy enhanced to be able to create a new map document.
8 Comments
ChrisFox
What if you had a blank MXD available with the tool or script that you just created a copy from and did what you needed to do?
GraemeBrowning
If that blank MXD was part of the ArcGIS install then I would consider it a solution but if you mean post-install having to ensure that one is made available on every machine my code gets run on then to me that's a workaround.

Its a shame the code below does not work - perhaps arcpy.mapping.MapDocument() can be enhanced to allow opening of the Normal.mxt?

import arcpy

mxd = arcpy.mapping.MapDocument(r"C:\Users\Graeme\AppData\Roaming\ESRI\Desktop10.1\ArcMap\Templates\Normal.mxt")
mxd.saveACopy(r"C:\temp\test.mxd")

ChrisFox
At 10.0 and 10.1 you will find many MXDs are available with the install at:

<ArcGIS Desktop Install>\MapTemplates

This gives you all sorts of MXDs with different layout sizes and orientation. In addition, depending on how you are sharing your code, if you are building a python add-in it is possible to package data including a blank MXD along with your add-in:

http://resources.arcgis.com/en/help/main/10.1/#/Essential_Python_add_in_concepts/014p0000001p000000/
GraemeBrowning
Thanks Chris

Those two recommendations should provide good workarounds (even a virtual solution).

However, it will probably just shift the blocking point for me to being why can't new layout elements and other objects like grids and graticules be created using ArcPy?
ChrisFox
Yes, unfortunately those are just not implemented. We did add the ability at 10.1 to clone a text or graphic element in the layout. So if one already exists you can clone a copy it and modify some properties.
GraemeBrowning
Cloning text and graphic elements so properties can be modified is certainly a help so thanks for reminding me of them.

The biggest one for me is not being able to change intervals on existing graticules using ArcPy - hopefully the ArcGIS Idea related to that will continue to attract votes.
SachinKanaujia
A must have !!!
maxsquires2

sources:

arcpy - Seeking Python script for creating .mxd files? - Geographic Information Systems Stack Exchan... 

pip - Installing python module within code - Stack Overflow 

python:

import arcpy
import subprocess
import sys

def install(package):
subprocess.call([sys.executable, "-m", "pip", "install", package])

install('comtypes')


import comtypes,os

def CreateMXD(path):
GetModule('esriCarto.olb')
import comtypes.gen.esriCarto as esriCarto
pMapDocument = CreateObject(esriCarto.MapDocument, esriCarto.IMapDocument)
pMapDocument.New(path)
pMapDocument.Save() #probably not required...

def GetLibPath():
""" Get the ArcObjects library path

It would be nice to just load the module directly instead of needing the path,
they are registered after all... But I just don't know enough about COM to do this

"""
compath=os.path.join(arcpy.GetInstallInfo()['InstallDir'],'com')
return compath

def GetModule(sModuleName):
""" Generate (if not already done) wrappers for COM modules
"""
from comtypes.client import GetModule
sLibPath = GetLibPath()
GetModule(os.path.join(sLibPath,sModuleName))

def CreateObject(COMClass, COMInterface):
""" Creates a new comtypes POINTER object where
COMClass is the class to be instantiated,
COMInterface is the interface to be assigned
"""
ptr = comtypes.client.CreateObject(COMClass, interface=COMInterface)
return ptr

if __name__=='__main__':
#testing...
arcpy.SetProduct('arcview')
filepath='c:/admin/testing123.mxd'
if os.path.exists(filepath):os.unlink(filepath)
CreateMXD(filepath)