exportToPDF Mapframe empty

06-24-2020 03:32 AM
New Contributor

Hi everyone,

I am using #Arcgis pro 2.5 and I have an issue :(

I am using the code above with a aprx with a map, a layout and some bookmarks.to export to pdf a layout zooming to differents boukmarks...

And it work unless I uncomment this line :

cursor = arcpy.da.SearchCursor(layerSet,"*")

Then, the mapframe is empty :(

But I want to iterate through Row objects and extract field values in my global code !

Any idea ?

import arcpy, os, time
from arcpy import env

# define the project
aprx = arcpy.mp.ArcGISProject("CURRENT")

# Define the first map
m = aprx.listMaps()[0]

# first layer in m
layerSet = m.listLayers()[0]
#cursor = arcpy.da.SearchCursor(layerSet,"*")

# define the first layout
layoutwork = aprx.listLayouts()[0]

# define first mapframe in the layout
mf = layoutwork.listElements("MAPFRAME_ELEMENT")[0]

# list of the bookmarks
bkmks = mf.map.listBookmarks()

#loop of the boukmarks
for bkmk in bkmks:
    # zoom to the bookmark
    # export in pdf
    layoutwork.exportToPDF(os.path.join(r"C:\temp", f"{bkmk.name}.pdf"))
0 Kudos
4 Replies
MVP Frequent Contributor

Hi, I'd probably investigate what the listlayers() is generating by throwing in a print statement.

The cursor should accept a layer object as input, but maybe try layer.dataSource instead (look up the syntax as my capital letters are usually mixed up...).

A final thought is that you may have to specify the dataframe object being passed into listlayers()

0 Kudos
New Contributor

Hi David,

Thank you for your answer ;)


Return the longName of the first layer ie : sel\Region


Return the 'short' name of the first layer : Region

cursor = arcpy.da.SearchCursor(layerSet.dataSource,"*")

Have the same result : the mapframe is empty :(

How do you do :

David Pike a écrit:

 specify the dataframe object being passed into listlayers()

Thank you

0 Kudos
MVP Frequent Contributor

oh I thought print(layerSet) would return an Object.

What are you intending to do with the cursor and the code in general? I cant see a use of the cursor after it's instantiated. Would give me a better insight when reading the code.

0 Kudos
New Contributor

Hi David,

This is my code :

import arcpy, os

from arcpy import env

# define the projet
aprx = arcpy.mp.ArcGISProject("CURRENT")

# Défine the map
m = aprx.listMaps("Departmental")[0]

# define the layout
layoutwork = aprx.listLayouts("MEP")[0]
# define the mapframe
mf = layoutwork.listElements("MAPFRAME_ELEMENT")[0]

# layers list in m(ap)
layerSets = m.listLayers()
# Region layout
layerRegion =  m.listLayers("Region")[0]

# Number of row in Region
result = arcpy.GetCount_management("Sel\Region")
nbregion = int(result.getOutput(0))
print("nb regions : "+str(nbregion))

# Loop Region layer
with arcpy.da.SearchCursor(layerRegion, 'admin1Name') as cursor:
    # for every région
    for row in cursor:
        LaRegion = row[0]
        print("--- Création des cartes pour : "+LaRegion)
        # find the bookmark of the region
        bkmks = mf.map.listBookmarks()
        for bkmk in bkmks:
            if bkmk.name == LaRegion:
        # loop for every layer in the map
        for layerSet1 in layerSets:
            # If The layer is in a group and it's not region
            if layerSet1.name != layerSet1.longName and layerSet1.name != "Region":
                # If the layer is gris
                if layerSet1.name == "gris":
                # If the layer is not gris
                    layerSet1.visible = True
                    ensDef = '"admin1Name" =\'' + LaRegion + '\''
                    layerSet1.definitionQuery = ensDef
                    NomFichier = "D:\\temp\\"+LaRegion+"_"+layerSet1.name+".pdf"
                    layerSet1.visible = False

Thank you for your help ;)

And the result :

0 Kudos