How to export a set of layers into multiple PDF(orAI)files, one layer at a time?

06-26-2014 07:07 AM
New Contributor
I am completely new to the arcpy.mapping module.I have a dataset contains files named as "1_A, 1_B, 1_C....12_A,12_B, 12_c,....200_A, 200_B, 200_C..etc."  I need to export each feature layer, one at a time , to a PDF or AI file through a COMPLETELY AUTOMATED process.  I tried to use Addlayer and Removelayer tool in Model Builder but they all run for one single feature layer once at a time and I can neither connect  the add and remove process to each other, nor could I connect the process through Iterator to the script tool anyway. This means although I use model builder or write python scrip, I have to change the layers (or the name of layers) for hundereds of times and save every layer into a new MXD file and export then to pdfs.

I was thinking about turnning on one of the layers at a time, export a .ai file with that layer visible, then turn that layer off and move on to the next layer in the list. I'm wondering if you have any suggestion about building the model or the scripts?

Thanks A Lot!!
0 Kudos
2 Replies
Occasional Contributor III
Try the following:

import arcpy, os, json, traceback, sys, datetime
from arcpy import mapping
from arcpy import env

mxd_out = r'D:\Temp\Blank.mxd'

now =
log = r'D:\Temp\Invertory_' + now.strftime("%Y-%m-%d" + '_' + "%H_%M_%S") + '_Error.log'

if arcpy.Exists(log):

if arcpy.Exists(mxd_out):

mapService = {}
jsonDump = json.dumps(mapService)
result = mapping.ConvertWebMapToMapDocument(jsonDump)
mxd = result.mapDocument
my_mxd = mxd.saveACopy(mxd_out)

env.workspace = r"D:\Temp\Layers"

contents = []

print 'Starting'

for dirpath, dirnames, datatypes in arcpy.da.Walk(env.workspace):

        for item in contents:
            if arcpy.Exists(item):
                arcpy.env.workspace = item

        listType = [arcpy.ListFeatureClasses, arcpy.ListDatasets]
        for list in listType:

            datasetList = list("*", 'All')

            # Iterate over the feature classes
            for dataset in datasetList:
                    data = item + '\\' + dataset

                    desc = arcpy.Describe(data)

                    print data + ' : ' + desc.dataType

                    # Crack open the map
                    mxd_new = arcpy.mapping.MapDocument(mxd_out)

                    df = arcpy.mapping.ListDataFrames(mxd_new, '*')[0]


                            lyrFile = arcpy.mapping.Layer(data)

                            if desc.dataType == 'RasterDataset':
                                layerType =
                                result = layerType(lyrFile, 'temp_layer')
                                layer_object = result.getOutput(0)
                                arcpy.mapping.AddLayer(df, layer_object)

                                result =, 'temp_layer')
                                layer_object = result.getOutput(0)
                                arcpy.mapping.AddLayer(df, layer_object)
                            # Set correct extent

                            nm = os.path.splitext(dataset)[0]

                            # Compute an output file name
                            out_file_name = (r"D:\Temp\Output" + "\\" + nm + '.jpg')

                            # Export Image of data frame
                            arcpy.mapping.ExportToJPEG(mxd_new, out_file_name, df, 300, 300, )

                            # Pull the layer out of the data frame to make room for the next one
                            arcpy.mapping.RemoveLayer(df, layer_object)

                            # Delete the GP layer


                        f=open(log, 'at')
                        f.write(data + '\n')

del mxd, mxd_new

if arcpy.Exists(mxd_out):

del mxd_out

There are a few additional items in there you won't need like the error log, but this should create you an output. You will need to change the ExportToJPEG to ExportToPDF command options.


0 Kudos
New Contributor
Thanks a lot!I'll try and reply ASAP.
0 Kudos