2

Based on Exporting set of layers into multiple PDF (or Adobe Illustrator) files, one at a time? I was able to alter and execute code that is trying to export 30+ layers from an MXD I have.

import arcpy

def turn_off_layers(mxd, df):
    for lyr in arcpy.mapping.ListLayers(mxd, "", df):
         lyr.visible = False
    mxd.save()

def print_layers(mxd,df):
    for i,lyr in enumerate(arcpy.mapping.ListLayers(mxd, "", df)):
         lyr.visible = True
         mxd.save() 
         arcpy.mapping.ExportToPDF(mxd, r"Y:\Desktop\Google Drive\DTI\Figures\%s.pdf" % lyr.name)
         lyr.visible = False
         mxd.save()

if __name__ == "__main__":
    mxd = arcpy.mapping.MapDocument(r"Y:\Desktop\Google Drive\DTI\DTI_Onondaga_100915.mxd")
    df = arcpy.mapping.ListDataFrames(mxd, "")[0]
    turn_off_layers(mxd, df)
    print_layers(mxd,df)

Unfortunately, the code runs without error and names the PDFs correctly, but every PDF is blank. Can anyone see where I am going wrong?
I am using ArcMap 10.2.2 on a 2011 iMac, running Parallels 9 in Cohesion view.

EDIT: Edited code based on Farid's and PolyGeo's suggestions:

import arcpy

def turn_off_layers(mxd, df):
    for lyr in arcpy.mapping.ListLayers(mxd, "", df):
         lyr.visible = False


def print_layers(mxd,df):
    for i,lyr in enumerate(arcpy.mapping.ListLayers(mxd, "", df)):
         print lyr.name
         lyr.visible = True
         df.extent = lyr.getExtent()
         arcpy.RefreshTOC()
         arcpy.RefreshActiveView()
         arcpy.mapping.ExportToPDF(mxd, r"Y:\Desktop\Google Drive\DTI\Figures\%s.pdf" % lyr.name)
         lyr.visible = False


if __name__ == "__main__":
    mxd = arcpy.mapping.MapDocument(r"CURRENT")
    df = arcpy.mapping.ListDataFrames(mxd, "")[0]
    turn_off_layers(mxd, df)
    print_layers(mxd,df)

AND THE EDITED CODE WORKS!

  • I am now trying this accessing an external drive that doesn't involved a desktop based folder on my mac side. – traggatmot Oct 9 '15 at 23:53
  • 1
    Try adding a print lyr.name line inside the for loop of your print_layers function to check that it is seeing your layers. – PolyGeo Oct 10 '15 at 0:00
2

Try this:

...
lyr.visible = True
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
mxd.save()
...

Open the mxd in Arcmap and:

mxd = arcpy.mapping.MapDocument(r"CURRENT")

and remove all mxd.save()

Last not the least, you may have overzoomed in your dataframe, it is safer to do zoom to layer extent in every iteration.

...
lyr.visible = True
df.extent= lyr.getExtent()
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
mxd.save()
...
  • Unfortunately, that didn't work either. I am still copying the files over to an external drive to test accessing, writing to/from there from a non-cohesive Windows view. – traggatmot Oct 10 '15 at 0:03
  • I don't think this a permission issue (due to the fact that pdfs are created). Use "CURRENT" for the mxd and check how it works – Farid Cheraghi Oct 10 '15 at 0:06
  • 1
    I'm going to edit the code based on your suggestions and repost - if you could, let me know if the edited code matches your suggestions. – traggatmot Oct 10 '15 at 0:20
  • Thanks so much! I want to ask you (though I will move to another question if you suggest) if there is a way to add a title and/or simple legend to each export PDF? – traggatmot Oct 10 '15 at 0:30
  • 1
    You could use DDPs (data driven pages). Do a google search; there are already lots of questions here at GIS SE. Then come up with some sample code and post it as another question – Farid Cheraghi Oct 10 '15 at 0:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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