Create new Map Document (*.mxd) using ArcPy

02-12-2013 03:24 PM
Status: Open
Labels (2)
Regular Contributor
I know that arcpy.mapping was not designed to author new objects - - but I wish that it had been and would like to see ArcPy enhanced to be able to create a new map document.
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?
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")

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:
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?
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.
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.
A must have !!!


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

pip - Installing python module within code - Stack Overflow 


import arcpy
import subprocess
import sys

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


import comtypes,os

def CreateMXD(path):
import comtypes.gen.esriCarto as esriCarto
pMapDocument = CreateObject(esriCarto.MapDocument, esriCarto.IMapDocument)
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

return compath

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

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__':
if os.path.exists(filepath):os.unlink(filepath)