I have found a solution to the issue below. The problem had to do with coding GetParameterAsText without actually using it as an input in a tool. That was the reason the raster layers wouldn't turn on.

I am fairly new to Python. Here is my question:

I have individual range maps for several hundred invertebrate species, each of which has the same extent. I would like to export these maps to .pdf using Python in ArcGIS 10.2. Each map also needs to have a "Places" and "Rivers" shapefile (these two shapefiles are the same for all of the species).

I have tried modifying the script from Exporting each layer in map to separate image using ArcPy?, but I can't seem to get the range map rasters to show up.

The following script needs work (and I must give credit to Roy, who answered the other post I mentioned), but this is what I have so far:

mxd = arcpy.mapping.MapDocument("CURRENT")  
df = arcpy.mapping.ListDataFrames(mxd, '')[0]

allLayers = arcpy.GetParameterAsText(0) 
lyrList = allLayers.split(";")

PDFPath = arcpy.GetParameterAsText(1)

for lyr in arcpy.mapping.ListLayers(mxd, '', df):
    for layer in lyrList:
        if lyr.name == "Places":
            lyr.visible = True
        if lyr.name == "Rivers":
            lyr.visible = True
        if lyr.name == layer:
            lyr.visible = True
            arcpy.mapping.ExportToPDF(mxd, os.path.join(r"C:\Project_7\Newer_data\Inverts\GO", PDFPath, lyr.name + ".pdf"), resolution=150) #EDIT: This line is working now.
            lyr.visible = False
    del mxd         

I don't think I have added the loops in the correct place. I need the "Places" and "Rivers" layers displayed on the map every time (i.e., for each range map).

I am unclear how to modify the following to fit my data:

arcpy.mapping.ExportToPNG(mxd, PDFPath+"\\" + lyr.name + ".pdf")

Specifically, what do I replace the "\" with? Do both backslashes need to be there? (i.e., PDFPath+"C:\Project_7\Newer_data\Inverts\GO\)

Also, I have tried just running the following code without the addition of the two shapefiles, but the layers don't seem to turn on.

for lyr in arcpy.mapping.ListLayers(mxd, '', df):
    for layer in lyrList:
        if lyr.name == layer:
            lyr.visible = True
  • So you want the layers defined in the tool parameter to be turned off if they are found in the map document?
    – artwork21
    Mar 23, 2015 at 12:39
  • Hi artwork21: Yes, I have all the layers in the map document. I would like to turn on an individual layer, export the map, and then turn off that layer. (However, I always need the "Places" and "Rivers" layers on.)
    – Henrik
    Mar 23, 2015 at 15:01
  • I see that I need to add a statement about removing the looped layers, but they aren't showing up in the first place.
    – Henrik
    Mar 23, 2015 at 15:20
  • I'm guessing you have a mismatch between lyr.name and layer value. Maybe do some print statements. Or add a print statement within if lyr.name == layer: condition.
    – artwork21
    Mar 23, 2015 at 16:02

2 Answers 2


I'm not sure what you're trying to do with the output file name, but constructing it like that will not work - the path needs to start with C:\.

Also, in Python it's good practice to use os.path.join to add paths together, e.g.

import os


arcpy.mapping.ExportToPDF(mxd, os.path.join(r"C:\Project_7\Newer_data\Inverts\GO", PDFPath, lyr.name + ".pdf"))

A single '\' in a string is considered an escape character, so you must either add an 'r' before the string so python treats it as a raw string, or use '\\'.

  • It's almost working now! All of the files get exported properly, but the rasters just don't show up. The "Places" and "Rivers" layers do show up, however. Does it have something to do with shapefiles vs. rasters? Can I use the script defining layers on rasters?
    – Henrik
    Mar 23, 2015 at 15:16
  • I got an error message: Runtime error Traceback (most recent call last): File "<string>", line 29, in <module> File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\utils.py", line 181, in fn_ return fn(*args, **kw) File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\mapping.py", line 1139, in ExportToPDF layout.exportToPDF(*args) AttributeError: Invalid destination path
    – Henrik
    Mar 23, 2015 at 15:25
  • From what I can tell, it's okay to use this script for rasters. I just don't know why they don't show up. Any suggestions? Thanks.
    – Henrik
    Mar 23, 2015 at 15:44
  • I think the destination path ("C:\Project_7\Newer_data\Inverts\GO" + PDFPath) needs to be created before you do the export. os.makedir should do this for you.
    – jon_two
    Mar 23, 2015 at 16:08
  • For sure. Thanks. I have updated the code above to incorporate this suggestion. I can now get all of the maps to export with the proper names, they just don't have any of the rasters showing up in them.
    – Henrik
    Mar 23, 2015 at 16:38

I'd use data driven pages, each covering the same extent of the map. You can show/hide layers in the view if there is a field to store pages name. Add field to every layer and populate it with relevant page name. Use definition query window of the layer to show/hide it using match/don't match options.

  • Thanks. I didn't think I could use data driven pages with rasters. I will take a look.
    – Henrik
    Mar 23, 2015 at 15:04
  • It won't work on rasters unless they stored in table catalog
    – FelixIP
    Mar 23, 2015 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.