Script for adding feature layers in batch to many mxd projects at once

137
2
Jump to solution
02-28-2024 12:16 PM
CarolinaDByl
New Contributor

Hi everyone, 

I will have to modify about 45 mxd projects and it will be very helpful if I can add a couple of new feature layers (located inside one file geodatabase) into all of them at once. 

I will be doubly helpful if I can set the formatting in the script as well. 

Have any of you done this before? I don't have anything to begin with because I'm not very good with python but I research quite a lot. I've found scripts to add many feature layers to one mxd, but I'm looking to have something that will run through all the mxd in a directory and add them without me opening each file. 

Thanks in advance 🙂

0 Kudos
2 Solutions

Accepted Solutions
Tom_Laue
New Contributor III

This is what I've used to loop through a directory of MXDs to examine and/or edit them...

 

 

import os, arcpy

# set a directory you're interested in
workingDir = r'C:\Users\tlaue\Desktop\GISTutorial'

arcpy.env.overwriteOutput = True

# get a list of all the files in the directory
maps = os.listdir(workingDir)

for m in maps:
    if m[-4:] == ".mxd":
        mxd = arcpy.mapping.MapDocument(os.path.join(workingDir,m))
        ##  DO SOMETHING HERE ##
        mxd.save()
                    

 

 

View solution in original post

gpopulis
New Contributor II

To add on to what Tom posted, create .lyr files for feature classes you want to add to the maps. If it's the same FCs on each one you should be able to add the .lyr file and resave/export your mxd.

If you have multiple FCs that will be using the same .lyr you can add the .lyr to your mxd and then reset the data source to whatever FC you want to use.

Sorry I can't add any code, the script I wrote that did this was for an old employer and I don't have access to it anymore.

Systems Analyst II
City of Ventura

View solution in original post

2 Replies
Tom_Laue
New Contributor III

This is what I've used to loop through a directory of MXDs to examine and/or edit them...

 

 

import os, arcpy

# set a directory you're interested in
workingDir = r'C:\Users\tlaue\Desktop\GISTutorial'

arcpy.env.overwriteOutput = True

# get a list of all the files in the directory
maps = os.listdir(workingDir)

for m in maps:
    if m[-4:] == ".mxd":
        mxd = arcpy.mapping.MapDocument(os.path.join(workingDir,m))
        ##  DO SOMETHING HERE ##
        mxd.save()
                    

 

 

gpopulis
New Contributor II

To add on to what Tom posted, create .lyr files for feature classes you want to add to the maps. If it's the same FCs on each one you should be able to add the .lyr file and resave/export your mxd.

If you have multiple FCs that will be using the same .lyr you can add the .lyr to your mxd and then reset the data source to whatever FC you want to use.

Sorry I can't add any code, the script I wrote that did this was for an old employer and I don't have access to it anymore.

Systems Analyst II
City of Ventura