Skip to main content
deleted 23 characters in body
Source Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

The following code contains more than you need, but it will show that you need to specify or create a blank mxd, then specify a data frame, then activate that data frame then add your layers/shapefiles with optional symbology specified

'''
mxd_create.py

Author:   [email protected]
Purpose:  create a base mxd and populate it with default values
Requires: specify the files to load and the default dataframes etc
template paths
ie
C:\Program Files (x86)\ArcGIS\Desktop10.2\MapTemplates\Traditional Layouts\LetterLandscape.mxd
C:\Program Files (x86)\ArcGIS\Desktop10.2\MapTemplates\Traditional Layouts\LetterPortrait.mxd
'''    
import arcpy
import sys
import os
import shutil

script = sys.argv[0]  # script name and location
mxd_src = "C:/!test/mapping/mxd_test02.mxd"  # a shell empty project
path, doc = os.path.split(mxd_src)
template = path + "/LetterPortrait.mxd"
shutil.copyfile(template,doc)

arcpy.env.workspace = path + "/shapefiles"
arcpy.overwriteOutputs = True

shp_files = ["AOI_mtm9.shp", "RandomPnts.shp"]
lyr_files = ["AOI_mtm9.lyr","RandomPnts.lyr"]
arcpy.ListFiles("*.lyr")    #use this to check above

author = "This could be you!!!!"
description = "Demo project working with the arcpy.mapping module and Python"
title = "Arcpy Mapping Demo:  Introduction to arcpy.mapping and Python interaction"
mxd = arcpy.mapping.MapDocument(doc)
mxd.author = author
mxd.description = description
mxd.title = title
mxd.relativePaths = True

df = arcpy.mapping.ListDataFrames(mxd)[0]   # list of data frames, 1 exists by default
df.name = "Sample files"                    # set its title
mxd.activeView = df.name                    # activate it

for shp in shp_files:                       # cycle through the list of shapefiles
  #fn, ext = os.path.splitext(f)            # for testing
  lyr = arcpy.mapping.Layer(shp)            # add the files and auto arrange
  arcpy.mapping.AddLayer(df,lyr,"AUTO_ARRANGE")     
arcpy.ApplySymbologyFromLayer_management(lyr,lyr_files[1])  # apply symbology from lyr files
#arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, True) 
arcpy.RefreshActiveView()
mxd.save()
del template, shp_files, lyr_files, lyr, df, mxd
os.startfile(doc)
del doc
##

Hope this helps

The following code contains more than you need, but it will show that you need to specify or create a blank mxd, then specify a data frame, then activate that data frame then add your layers/shapefiles with optional symbology specified

'''
mxd_create.py

Author:   [email protected]
Purpose:  create a base mxd and populate it with default values
Requires: specify the files to load and the default dataframes etc
template paths
ie
C:\Program Files (x86)\ArcGIS\Desktop10.2\MapTemplates\Traditional Layouts\LetterLandscape.mxd
C:\Program Files (x86)\ArcGIS\Desktop10.2\MapTemplates\Traditional Layouts\LetterPortrait.mxd
'''    
import arcpy
import sys
import os
import shutil

script = sys.argv[0]  # script name and location
mxd_src = "C:/!test/mapping/mxd_test02.mxd"  # a shell empty project
path, doc = os.path.split(mxd_src)
template = path + "/LetterPortrait.mxd"
shutil.copyfile(template,doc)

arcpy.env.workspace = path + "/shapefiles"
arcpy.overwriteOutputs = True

shp_files = ["AOI_mtm9.shp", "RandomPnts.shp"]
lyr_files = ["AOI_mtm9.lyr","RandomPnts.lyr"]
arcpy.ListFiles("*.lyr")    #use this to check above

author = "This could be you!!!!"
description = "Demo project working with the arcpy.mapping module and Python"
title = "Arcpy Mapping Demo:  Introduction to arcpy.mapping and Python interaction"
mxd = arcpy.mapping.MapDocument(doc)
mxd.author = author
mxd.description = description
mxd.title = title
mxd.relativePaths = True

df = arcpy.mapping.ListDataFrames(mxd)[0]   # list of data frames, 1 exists by default
df.name = "Sample files"                    # set its title
mxd.activeView = df.name                    # activate it

for shp in shp_files:                       # cycle through the list of shapefiles
  #fn, ext = os.path.splitext(f)            # for testing
  lyr = arcpy.mapping.Layer(shp)            # add the files and auto arrange
  arcpy.mapping.AddLayer(df,lyr,"AUTO_ARRANGE")     
arcpy.ApplySymbologyFromLayer_management(lyr,lyr_files[1])  # apply symbology from lyr files
#arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, True) 
arcpy.RefreshActiveView()
mxd.save()
del template, shp_files, lyr_files, lyr, df, mxd
os.startfile(doc)
del doc
##

Hope this helps

The following code contains more than you need, but it will show that you need to specify or create a blank mxd, then specify a data frame, then activate that data frame then add your layers/shapefiles with optional symbology specified

'''
mxd_create.py

Author:   [email protected]
Purpose:  create a base mxd and populate it with default values
Requires: specify the files to load and the default dataframes etc
template paths
ie
C:\Program Files (x86)\ArcGIS\Desktop10.2\MapTemplates\Traditional Layouts\LetterLandscape.mxd
C:\Program Files (x86)\ArcGIS\Desktop10.2\MapTemplates\Traditional Layouts\LetterPortrait.mxd
'''    
import arcpy
import sys
import os
import shutil

script = sys.argv[0]  # script name and location
mxd_src = "C:/!test/mapping/mxd_test02.mxd"  # a shell empty project
path, doc = os.path.split(mxd_src)
template = path + "/LetterPortrait.mxd"
shutil.copyfile(template,doc)

arcpy.env.workspace = path + "/shapefiles"
arcpy.overwriteOutputs = True

shp_files = ["AOI_mtm9.shp", "RandomPnts.shp"]
lyr_files = ["AOI_mtm9.lyr","RandomPnts.lyr"]
arcpy.ListFiles("*.lyr")    #use this to check above

author = "This could be you!!!!"
description = "Demo project working with the arcpy.mapping module and Python"
title = "Arcpy Mapping Demo:  Introduction to arcpy.mapping and Python interaction"
mxd = arcpy.mapping.MapDocument(doc)
mxd.author = author
mxd.description = description
mxd.title = title
mxd.relativePaths = True

df = arcpy.mapping.ListDataFrames(mxd)[0]   # list of data frames, 1 exists by default
df.name = "Sample files"                    # set its title
mxd.activeView = df.name                    # activate it

for shp in shp_files:                       # cycle through the list of shapefiles
  #fn, ext = os.path.splitext(f)            # for testing
  lyr = arcpy.mapping.Layer(shp)            # add the files and auto arrange
  arcpy.mapping.AddLayer(df,lyr,"AUTO_ARRANGE")     
arcpy.ApplySymbologyFromLayer_management(lyr,lyr_files[1])  # apply symbology from lyr files
#arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, True) 
arcpy.RefreshActiveView()
mxd.save()
del template, shp_files, lyr_files, lyr, df, mxd
os.startfile(doc)
del doc
##
Post Undeleted by PolyGeo
Post Deleted by user681
Source Link
user681
user681

The following code contains more than you need, but it will show that you need to specify or create a blank mxd, then specify a data frame, then activate that data frame then add your layers/shapefiles with optional symbology specified

'''
mxd_create.py

Author:   [email protected]
Purpose:  create a base mxd and populate it with default values
Requires: specify the files to load and the default dataframes etc
template paths
ie
C:\Program Files (x86)\ArcGIS\Desktop10.2\MapTemplates\Traditional Layouts\LetterLandscape.mxd
C:\Program Files (x86)\ArcGIS\Desktop10.2\MapTemplates\Traditional Layouts\LetterPortrait.mxd
'''    
import arcpy
import sys
import os
import shutil

script = sys.argv[0]  # script name and location
mxd_src = "C:/!test/mapping/mxd_test02.mxd"  # a shell empty project
path, doc = os.path.split(mxd_src)
template = path + "/LetterPortrait.mxd"
shutil.copyfile(template,doc)

arcpy.env.workspace = path + "/shapefiles"
arcpy.overwriteOutputs = True

shp_files = ["AOI_mtm9.shp", "RandomPnts.shp"]
lyr_files = ["AOI_mtm9.lyr","RandomPnts.lyr"]
arcpy.ListFiles("*.lyr")    #use this to check above

author = "This could be you!!!!"
description = "Demo project working with the arcpy.mapping module and Python"
title = "Arcpy Mapping Demo:  Introduction to arcpy.mapping and Python interaction"
mxd = arcpy.mapping.MapDocument(doc)
mxd.author = author
mxd.description = description
mxd.title = title
mxd.relativePaths = True

df = arcpy.mapping.ListDataFrames(mxd)[0]   # list of data frames, 1 exists by default
df.name = "Sample files"                    # set its title
mxd.activeView = df.name                    # activate it

for shp in shp_files:                       # cycle through the list of shapefiles
  #fn, ext = os.path.splitext(f)            # for testing
  lyr = arcpy.mapping.Layer(shp)            # add the files and auto arrange
  arcpy.mapping.AddLayer(df,lyr,"AUTO_ARRANGE")     
arcpy.ApplySymbologyFromLayer_management(lyr,lyr_files[1])  # apply symbology from lyr files
#arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, True) 
arcpy.RefreshActiveView()
mxd.save()
del template, shp_files, lyr_files, lyr, df, mxd
os.startfile(doc)
del doc
##

Hope this helps