Iterating folder of shapefiles using ArcPy and exporting mxd to PDF Planned maintenance...

Special flights

GDP with Intermediate Production

What initially awakened the Balrog?

If Windows 7 doesn't support WSL, then what is "Subsystem for UNIX-based Applications"?

Test print coming out spongy

Why is the change of basis formula counter-intuitive? [See details]

Did any compiler fully use 80-bit floating point?

Why complex landing gears are used instead of simple,reliability and light weight muscle wire or shape memory alloys?

AppleTVs create a chatty alternate WiFi network

Trying to understand entropy as a novice in thermodynamics

My mentor says to set image to Fine instead of RAW — how is this different from JPG?

what is the log of the PDF for a Normal Distribution?

What is the chair depicted in Cesare Maccari's 1889 painting "Cicerone denuncia Catilina"?

The Nth Gryphon Number

Is openssl rand command cryptographically secure?

Simple Http Server

Why is std::move not [[nodiscard]] in C++20?

How does light 'choose' between wave and particle behaviour?

Was Kant an Intuitionist about mathematical objects?

How can a team of shapeshifters communicate?

The test team as an enemy of development? And how can this be avoided?

How to ternary Plot3D a function

Printing attributes of selection in ArcPy?

Why datecode is SO IMPORTANT to chip manufacturers?



Iterating folder of shapefiles using ArcPy and exporting mxd to PDF



Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Adding shapefile or feature class as layer in ArcGIS Desktop using Python/ArcPy?Add a .lyr file to multiple mxd's within a specified folder using pythonListing field name aliases in one shapefile/layer using ArcPy?ArcPy: (SearchCursor/export to tiff ) with no outputHow to export a MXD to a PDF but only switching the folderArcMap Export PDF vs arcpy Export PDF scale differencesIterating through all geometries using ArcPy search cursor?ArcGIS 10 arcpy to add a WMS Service to MXDClip each layer in mxd with ArcpyExporting jpeg around polygon boundary using Python?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















I'm new to Python and just created a hard coded script for the process I want, and it works. Right now it calls on the one of three shapefile I have in this map document. I want it to do the same thing, but for each shapefile in this folder. I have no idea how to go about this! I always use ModelBuilder, so I know that is should be an iteration.



Does anyone have any idea how to go about this, or how I am supposed to change this code?



enter image description here



import arcpy

#Declaring Variables
ID = arcpy.GetParameterAsText(0)
OutputLocation = arcpy.GetParameterAsText(1)

mxd = arcpy.mapping.MapDocument(r"C:Usersm3rexkacDesktopRename_TestDOE_auto.mxd")

df = arcpy.mapping.ListDataFrames(mxd,"*")[0]

#Now to Create a Layer Object, the line of code below returns a list of all the layers in the dataframe, with this name. The [0] returns the first layer with that name instead of the whole list
Layer = arcpy.mapping.ListLayers(mxd, "Mary_Creek_Co_Parcels", df)[0]

#This zooms the dataframe to the selected feature
lyr = arcpy.mapping.ListLayers(mxd,"Mary_Creek_Co_Parcels", df)[0]
extent = lyr.getExtent()

df.extent = extent
df.scale = scale


#Exporting map dataframe view to pdf, I have it set up to save
arcpy.mapping.ExportToPDF(mxd, OutputLocation + "2.pdf", "Page_Layout", 640, 480)

#Cleaning out memory by deleting the python objects
del mxd
del df
del Layer









share|improve this question
















bumped to the homepage by Community 21 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 2





    Welcome! Please confirm what you are trying to do with the script - it looks like you are attempting to export a PDF of a map, but you want to do this for a number of shapefiles that do not exist as a map. Do you wish to iteratively add each shapefile to the map, save it, export it, and remove it before moving on to the next one?

    – smiller
    Nov 13 '18 at 14:25











  • Yes that's exactly what I'm trying to do.

    – Kelsey Ciarrocca
    Nov 15 '18 at 18:28











  • I have about 300 shapefiles that I need to produce a pdf of and am trying to find the best way to do that.

    – Kelsey Ciarrocca
    Nov 15 '18 at 18:29











  • Check out arcpy.mapping.Layer and arcpy.mapping.AddLayer, such as in this thread: gis.stackexchange.com/questions/4882 and this documentation page: desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-mapping/…. What have you tried to identify the shapefiles? I use glob, but you can also use arcpy.ListFeatureClasses or arcpy.da.Walk.

    – smiller
    Nov 15 '18 at 18:44


















0















I'm new to Python and just created a hard coded script for the process I want, and it works. Right now it calls on the one of three shapefile I have in this map document. I want it to do the same thing, but for each shapefile in this folder. I have no idea how to go about this! I always use ModelBuilder, so I know that is should be an iteration.



Does anyone have any idea how to go about this, or how I am supposed to change this code?



enter image description here



import arcpy

#Declaring Variables
ID = arcpy.GetParameterAsText(0)
OutputLocation = arcpy.GetParameterAsText(1)

mxd = arcpy.mapping.MapDocument(r"C:Usersm3rexkacDesktopRename_TestDOE_auto.mxd")

df = arcpy.mapping.ListDataFrames(mxd,"*")[0]

#Now to Create a Layer Object, the line of code below returns a list of all the layers in the dataframe, with this name. The [0] returns the first layer with that name instead of the whole list
Layer = arcpy.mapping.ListLayers(mxd, "Mary_Creek_Co_Parcels", df)[0]

#This zooms the dataframe to the selected feature
lyr = arcpy.mapping.ListLayers(mxd,"Mary_Creek_Co_Parcels", df)[0]
extent = lyr.getExtent()

df.extent = extent
df.scale = scale


#Exporting map dataframe view to pdf, I have it set up to save
arcpy.mapping.ExportToPDF(mxd, OutputLocation + "2.pdf", "Page_Layout", 640, 480)

#Cleaning out memory by deleting the python objects
del mxd
del df
del Layer









share|improve this question
















bumped to the homepage by Community 21 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 2





    Welcome! Please confirm what you are trying to do with the script - it looks like you are attempting to export a PDF of a map, but you want to do this for a number of shapefiles that do not exist as a map. Do you wish to iteratively add each shapefile to the map, save it, export it, and remove it before moving on to the next one?

    – smiller
    Nov 13 '18 at 14:25











  • Yes that's exactly what I'm trying to do.

    – Kelsey Ciarrocca
    Nov 15 '18 at 18:28











  • I have about 300 shapefiles that I need to produce a pdf of and am trying to find the best way to do that.

    – Kelsey Ciarrocca
    Nov 15 '18 at 18:29











  • Check out arcpy.mapping.Layer and arcpy.mapping.AddLayer, such as in this thread: gis.stackexchange.com/questions/4882 and this documentation page: desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-mapping/…. What have you tried to identify the shapefiles? I use glob, but you can also use arcpy.ListFeatureClasses or arcpy.da.Walk.

    – smiller
    Nov 15 '18 at 18:44














0












0








0








I'm new to Python and just created a hard coded script for the process I want, and it works. Right now it calls on the one of three shapefile I have in this map document. I want it to do the same thing, but for each shapefile in this folder. I have no idea how to go about this! I always use ModelBuilder, so I know that is should be an iteration.



Does anyone have any idea how to go about this, or how I am supposed to change this code?



enter image description here



import arcpy

#Declaring Variables
ID = arcpy.GetParameterAsText(0)
OutputLocation = arcpy.GetParameterAsText(1)

mxd = arcpy.mapping.MapDocument(r"C:Usersm3rexkacDesktopRename_TestDOE_auto.mxd")

df = arcpy.mapping.ListDataFrames(mxd,"*")[0]

#Now to Create a Layer Object, the line of code below returns a list of all the layers in the dataframe, with this name. The [0] returns the first layer with that name instead of the whole list
Layer = arcpy.mapping.ListLayers(mxd, "Mary_Creek_Co_Parcels", df)[0]

#This zooms the dataframe to the selected feature
lyr = arcpy.mapping.ListLayers(mxd,"Mary_Creek_Co_Parcels", df)[0]
extent = lyr.getExtent()

df.extent = extent
df.scale = scale


#Exporting map dataframe view to pdf, I have it set up to save
arcpy.mapping.ExportToPDF(mxd, OutputLocation + "2.pdf", "Page_Layout", 640, 480)

#Cleaning out memory by deleting the python objects
del mxd
del df
del Layer









share|improve this question
















I'm new to Python and just created a hard coded script for the process I want, and it works. Right now it calls on the one of three shapefile I have in this map document. I want it to do the same thing, but for each shapefile in this folder. I have no idea how to go about this! I always use ModelBuilder, so I know that is should be an iteration.



Does anyone have any idea how to go about this, or how I am supposed to change this code?



enter image description here



import arcpy

#Declaring Variables
ID = arcpy.GetParameterAsText(0)
OutputLocation = arcpy.GetParameterAsText(1)

mxd = arcpy.mapping.MapDocument(r"C:Usersm3rexkacDesktopRename_TestDOE_auto.mxd")

df = arcpy.mapping.ListDataFrames(mxd,"*")[0]

#Now to Create a Layer Object, the line of code below returns a list of all the layers in the dataframe, with this name. The [0] returns the first layer with that name instead of the whole list
Layer = arcpy.mapping.ListLayers(mxd, "Mary_Creek_Co_Parcels", df)[0]

#This zooms the dataframe to the selected feature
lyr = arcpy.mapping.ListLayers(mxd,"Mary_Creek_Co_Parcels", df)[0]
extent = lyr.getExtent()

df.extent = extent
df.scale = scale


#Exporting map dataframe view to pdf, I have it set up to save
arcpy.mapping.ExportToPDF(mxd, OutputLocation + "2.pdf", "Page_Layout", 640, 480)

#Cleaning out memory by deleting the python objects
del mxd
del df
del Layer






arcpy shapefile pdf mxd iteration






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 20:51









smiller

2,409217




2,409217










asked Nov 13 '18 at 13:56









Kelsey CiarroccaKelsey Ciarrocca

11




11





bumped to the homepage by Community 21 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 21 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.










  • 2





    Welcome! Please confirm what you are trying to do with the script - it looks like you are attempting to export a PDF of a map, but you want to do this for a number of shapefiles that do not exist as a map. Do you wish to iteratively add each shapefile to the map, save it, export it, and remove it before moving on to the next one?

    – smiller
    Nov 13 '18 at 14:25











  • Yes that's exactly what I'm trying to do.

    – Kelsey Ciarrocca
    Nov 15 '18 at 18:28











  • I have about 300 shapefiles that I need to produce a pdf of and am trying to find the best way to do that.

    – Kelsey Ciarrocca
    Nov 15 '18 at 18:29











  • Check out arcpy.mapping.Layer and arcpy.mapping.AddLayer, such as in this thread: gis.stackexchange.com/questions/4882 and this documentation page: desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-mapping/…. What have you tried to identify the shapefiles? I use glob, but you can also use arcpy.ListFeatureClasses or arcpy.da.Walk.

    – smiller
    Nov 15 '18 at 18:44














  • 2





    Welcome! Please confirm what you are trying to do with the script - it looks like you are attempting to export a PDF of a map, but you want to do this for a number of shapefiles that do not exist as a map. Do you wish to iteratively add each shapefile to the map, save it, export it, and remove it before moving on to the next one?

    – smiller
    Nov 13 '18 at 14:25











  • Yes that's exactly what I'm trying to do.

    – Kelsey Ciarrocca
    Nov 15 '18 at 18:28











  • I have about 300 shapefiles that I need to produce a pdf of and am trying to find the best way to do that.

    – Kelsey Ciarrocca
    Nov 15 '18 at 18:29











  • Check out arcpy.mapping.Layer and arcpy.mapping.AddLayer, such as in this thread: gis.stackexchange.com/questions/4882 and this documentation page: desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-mapping/…. What have you tried to identify the shapefiles? I use glob, but you can also use arcpy.ListFeatureClasses or arcpy.da.Walk.

    – smiller
    Nov 15 '18 at 18:44








2




2





Welcome! Please confirm what you are trying to do with the script - it looks like you are attempting to export a PDF of a map, but you want to do this for a number of shapefiles that do not exist as a map. Do you wish to iteratively add each shapefile to the map, save it, export it, and remove it before moving on to the next one?

– smiller
Nov 13 '18 at 14:25





Welcome! Please confirm what you are trying to do with the script - it looks like you are attempting to export a PDF of a map, but you want to do this for a number of shapefiles that do not exist as a map. Do you wish to iteratively add each shapefile to the map, save it, export it, and remove it before moving on to the next one?

– smiller
Nov 13 '18 at 14:25













Yes that's exactly what I'm trying to do.

– Kelsey Ciarrocca
Nov 15 '18 at 18:28





Yes that's exactly what I'm trying to do.

– Kelsey Ciarrocca
Nov 15 '18 at 18:28













I have about 300 shapefiles that I need to produce a pdf of and am trying to find the best way to do that.

– Kelsey Ciarrocca
Nov 15 '18 at 18:29





I have about 300 shapefiles that I need to produce a pdf of and am trying to find the best way to do that.

– Kelsey Ciarrocca
Nov 15 '18 at 18:29













Check out arcpy.mapping.Layer and arcpy.mapping.AddLayer, such as in this thread: gis.stackexchange.com/questions/4882 and this documentation page: desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-mapping/…. What have you tried to identify the shapefiles? I use glob, but you can also use arcpy.ListFeatureClasses or arcpy.da.Walk.

– smiller
Nov 15 '18 at 18:44





Check out arcpy.mapping.Layer and arcpy.mapping.AddLayer, such as in this thread: gis.stackexchange.com/questions/4882 and this documentation page: desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-mapping/…. What have you tried to identify the shapefiles? I use glob, but you can also use arcpy.ListFeatureClasses or arcpy.da.Walk.

– smiller
Nov 15 '18 at 18:44










1 Answer
1






active

oldest

votes


















0














First, you need to get a list of the shapefiles in the folder to iterate through. I like to use the Python library glob for this as it's pretty fast even when filtering for specific file naming conventions, but arcpy.ListFeatureClasses() or arcpy.da.Walk() would also work (note glob works for file system access and does well with shapefiles, but wouldn't work in a file geodatabase).



Documentation:




  • da.Walk()

  • glob

  • ListFeatureClasses()


An example using glob and os:



shplist = glob.glob(os.path.join(pathtoshapefiles, "*.shp"))
shplist.sort() # glob returns an unsorted list


Esri's example using ListFeatureClasses:



import os
import arcpy

# Set the workspace for ListFeatureClasses
arcpy.env.workspace = "c:/base"

# Use the ListFeatureClasses function to return a list of
# shapefiles.
featureclasses = arcpy.ListFeatureClasses()


Once you have the list of shapefiles, iterate through them. You will add the new layer, zoom to the desired extent, export the PDF, then remove the new layer. I've assumed based on your responses above that you will be using the same MXD each time, and used your code above as a base. I don't have a big stack of shapefiles to test so it may need some additional tweaking.



ID = arcpy.GetParameterAsText(0)  
OutputLocation = arcpy.GetParameterAsText(1)

mxd = arcpy.mapping.MapDocument(r"C:Usersm3rexkacDesktopRename_TestDOE_auto.mxd")

df = arcpy.mapping.ListDataFrames(mxd,"*")[0]

for shppath in shplist:
# create a new layer
newlayer = arcpy.mapping.Layer(shppath)
# Add the new layer to the MXD
arcpy.mapping.AddLayer(df, newlayer,"BOTTOM")

#This zooms the dataframe to the selected feature
lyr = arcpy.mapping.ListLayers(mxd,"Mary_Creek_Co_Parcels", df)[0] # May need to change this line to reflec the correct layer name.
extent = lyr.getExtent()

df.extent = extent
df.scale = scale

#Exporting map dataframe view to pdf
mxd.save()
arcpy.mapping.ExportToPDF(mxd, OutputLocation + "_" + str(n) + ".pdf", "Page_Layout", 640, 480)
arcpy.mapping.RemoveLayer(df, lyr)
del mxd





share|improve this answer
























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "79"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f302455%2fiterating-folder-of-shapefiles-using-arcpy-and-exporting-mxd-to-pdf%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    First, you need to get a list of the shapefiles in the folder to iterate through. I like to use the Python library glob for this as it's pretty fast even when filtering for specific file naming conventions, but arcpy.ListFeatureClasses() or arcpy.da.Walk() would also work (note glob works for file system access and does well with shapefiles, but wouldn't work in a file geodatabase).



    Documentation:




    • da.Walk()

    • glob

    • ListFeatureClasses()


    An example using glob and os:



    shplist = glob.glob(os.path.join(pathtoshapefiles, "*.shp"))
    shplist.sort() # glob returns an unsorted list


    Esri's example using ListFeatureClasses:



    import os
    import arcpy

    # Set the workspace for ListFeatureClasses
    arcpy.env.workspace = "c:/base"

    # Use the ListFeatureClasses function to return a list of
    # shapefiles.
    featureclasses = arcpy.ListFeatureClasses()


    Once you have the list of shapefiles, iterate through them. You will add the new layer, zoom to the desired extent, export the PDF, then remove the new layer. I've assumed based on your responses above that you will be using the same MXD each time, and used your code above as a base. I don't have a big stack of shapefiles to test so it may need some additional tweaking.



    ID = arcpy.GetParameterAsText(0)  
    OutputLocation = arcpy.GetParameterAsText(1)

    mxd = arcpy.mapping.MapDocument(r"C:Usersm3rexkacDesktopRename_TestDOE_auto.mxd")

    df = arcpy.mapping.ListDataFrames(mxd,"*")[0]

    for shppath in shplist:
    # create a new layer
    newlayer = arcpy.mapping.Layer(shppath)
    # Add the new layer to the MXD
    arcpy.mapping.AddLayer(df, newlayer,"BOTTOM")

    #This zooms the dataframe to the selected feature
    lyr = arcpy.mapping.ListLayers(mxd,"Mary_Creek_Co_Parcels", df)[0] # May need to change this line to reflec the correct layer name.
    extent = lyr.getExtent()

    df.extent = extent
    df.scale = scale

    #Exporting map dataframe view to pdf
    mxd.save()
    arcpy.mapping.ExportToPDF(mxd, OutputLocation + "_" + str(n) + ".pdf", "Page_Layout", 640, 480)
    arcpy.mapping.RemoveLayer(df, lyr)
    del mxd





    share|improve this answer




























      0














      First, you need to get a list of the shapefiles in the folder to iterate through. I like to use the Python library glob for this as it's pretty fast even when filtering for specific file naming conventions, but arcpy.ListFeatureClasses() or arcpy.da.Walk() would also work (note glob works for file system access and does well with shapefiles, but wouldn't work in a file geodatabase).



      Documentation:




      • da.Walk()

      • glob

      • ListFeatureClasses()


      An example using glob and os:



      shplist = glob.glob(os.path.join(pathtoshapefiles, "*.shp"))
      shplist.sort() # glob returns an unsorted list


      Esri's example using ListFeatureClasses:



      import os
      import arcpy

      # Set the workspace for ListFeatureClasses
      arcpy.env.workspace = "c:/base"

      # Use the ListFeatureClasses function to return a list of
      # shapefiles.
      featureclasses = arcpy.ListFeatureClasses()


      Once you have the list of shapefiles, iterate through them. You will add the new layer, zoom to the desired extent, export the PDF, then remove the new layer. I've assumed based on your responses above that you will be using the same MXD each time, and used your code above as a base. I don't have a big stack of shapefiles to test so it may need some additional tweaking.



      ID = arcpy.GetParameterAsText(0)  
      OutputLocation = arcpy.GetParameterAsText(1)

      mxd = arcpy.mapping.MapDocument(r"C:Usersm3rexkacDesktopRename_TestDOE_auto.mxd")

      df = arcpy.mapping.ListDataFrames(mxd,"*")[0]

      for shppath in shplist:
      # create a new layer
      newlayer = arcpy.mapping.Layer(shppath)
      # Add the new layer to the MXD
      arcpy.mapping.AddLayer(df, newlayer,"BOTTOM")

      #This zooms the dataframe to the selected feature
      lyr = arcpy.mapping.ListLayers(mxd,"Mary_Creek_Co_Parcels", df)[0] # May need to change this line to reflec the correct layer name.
      extent = lyr.getExtent()

      df.extent = extent
      df.scale = scale

      #Exporting map dataframe view to pdf
      mxd.save()
      arcpy.mapping.ExportToPDF(mxd, OutputLocation + "_" + str(n) + ".pdf", "Page_Layout", 640, 480)
      arcpy.mapping.RemoveLayer(df, lyr)
      del mxd





      share|improve this answer


























        0












        0








        0







        First, you need to get a list of the shapefiles in the folder to iterate through. I like to use the Python library glob for this as it's pretty fast even when filtering for specific file naming conventions, but arcpy.ListFeatureClasses() or arcpy.da.Walk() would also work (note glob works for file system access and does well with shapefiles, but wouldn't work in a file geodatabase).



        Documentation:




        • da.Walk()

        • glob

        • ListFeatureClasses()


        An example using glob and os:



        shplist = glob.glob(os.path.join(pathtoshapefiles, "*.shp"))
        shplist.sort() # glob returns an unsorted list


        Esri's example using ListFeatureClasses:



        import os
        import arcpy

        # Set the workspace for ListFeatureClasses
        arcpy.env.workspace = "c:/base"

        # Use the ListFeatureClasses function to return a list of
        # shapefiles.
        featureclasses = arcpy.ListFeatureClasses()


        Once you have the list of shapefiles, iterate through them. You will add the new layer, zoom to the desired extent, export the PDF, then remove the new layer. I've assumed based on your responses above that you will be using the same MXD each time, and used your code above as a base. I don't have a big stack of shapefiles to test so it may need some additional tweaking.



        ID = arcpy.GetParameterAsText(0)  
        OutputLocation = arcpy.GetParameterAsText(1)

        mxd = arcpy.mapping.MapDocument(r"C:Usersm3rexkacDesktopRename_TestDOE_auto.mxd")

        df = arcpy.mapping.ListDataFrames(mxd,"*")[0]

        for shppath in shplist:
        # create a new layer
        newlayer = arcpy.mapping.Layer(shppath)
        # Add the new layer to the MXD
        arcpy.mapping.AddLayer(df, newlayer,"BOTTOM")

        #This zooms the dataframe to the selected feature
        lyr = arcpy.mapping.ListLayers(mxd,"Mary_Creek_Co_Parcels", df)[0] # May need to change this line to reflec the correct layer name.
        extent = lyr.getExtent()

        df.extent = extent
        df.scale = scale

        #Exporting map dataframe view to pdf
        mxd.save()
        arcpy.mapping.ExportToPDF(mxd, OutputLocation + "_" + str(n) + ".pdf", "Page_Layout", 640, 480)
        arcpy.mapping.RemoveLayer(df, lyr)
        del mxd





        share|improve this answer













        First, you need to get a list of the shapefiles in the folder to iterate through. I like to use the Python library glob for this as it's pretty fast even when filtering for specific file naming conventions, but arcpy.ListFeatureClasses() or arcpy.da.Walk() would also work (note glob works for file system access and does well with shapefiles, but wouldn't work in a file geodatabase).



        Documentation:




        • da.Walk()

        • glob

        • ListFeatureClasses()


        An example using glob and os:



        shplist = glob.glob(os.path.join(pathtoshapefiles, "*.shp"))
        shplist.sort() # glob returns an unsorted list


        Esri's example using ListFeatureClasses:



        import os
        import arcpy

        # Set the workspace for ListFeatureClasses
        arcpy.env.workspace = "c:/base"

        # Use the ListFeatureClasses function to return a list of
        # shapefiles.
        featureclasses = arcpy.ListFeatureClasses()


        Once you have the list of shapefiles, iterate through them. You will add the new layer, zoom to the desired extent, export the PDF, then remove the new layer. I've assumed based on your responses above that you will be using the same MXD each time, and used your code above as a base. I don't have a big stack of shapefiles to test so it may need some additional tweaking.



        ID = arcpy.GetParameterAsText(0)  
        OutputLocation = arcpy.GetParameterAsText(1)

        mxd = arcpy.mapping.MapDocument(r"C:Usersm3rexkacDesktopRename_TestDOE_auto.mxd")

        df = arcpy.mapping.ListDataFrames(mxd,"*")[0]

        for shppath in shplist:
        # create a new layer
        newlayer = arcpy.mapping.Layer(shppath)
        # Add the new layer to the MXD
        arcpy.mapping.AddLayer(df, newlayer,"BOTTOM")

        #This zooms the dataframe to the selected feature
        lyr = arcpy.mapping.ListLayers(mxd,"Mary_Creek_Co_Parcels", df)[0] # May need to change this line to reflec the correct layer name.
        extent = lyr.getExtent()

        df.extent = extent
        df.scale = scale

        #Exporting map dataframe view to pdf
        mxd.save()
        arcpy.mapping.ExportToPDF(mxd, OutputLocation + "_" + str(n) + ".pdf", "Page_Layout", 640, 480)
        arcpy.mapping.RemoveLayer(df, lyr)
        del mxd






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 19:15









        smillersmiller

        2,409217




        2,409217






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f302455%2fiterating-folder-of-shapefiles-using-arcpy-and-exporting-mxd-to-pdf%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Щит и меч (фильм) Содержание Названия серий | Сюжет |...

            Венесуэла на летних Олимпийских играх 2000 Содержание Состав...

            Meter-Bus Содержание Параметры шины | Стандартизация |...