I'm running this script to iterate through layers, zoom to them, then export each as a pdf but I keep getting this error:

AttributeError: 'str' object has no attribute '_arc_object'

I'm not skilled enough at python to interpret this error message.

Here's my code:

import arcpy
def zoom_export(zlayer,pdfname,ratio):
    zoom = arcpy.mapping.Layer(zlayer)
    df.extent = zoom.getSelectedExtent()   #zooming to selected feature layer
    df.scale = df.scale * ratio
    zoomin = arcpy.mapping.ExportToPDF("C:\Users\TSPA standard\Desktop\gis\Test.mxd", "TESTPDF")
    arcpy.SelectLayerByAttribute_management(zoom, "CLEAR_SELECTION")

def turn_on(layer,tf,mxd):
    mxd = "C:\Users\TSPA standard\Desktop\gis\Test.mxd"
    df = arcpy.mapping.ListDataFrames(mxd)[0]
    layers = arcpy.mapping.ListLayers(mxd, "*", df)
    for x in layers:
        if x.name == layer:
            print x.name
            x.visible = tf

mxd = arcpy.mapping.MapDocument(r"C:\Users\TSPA standard\Desktop\gis\Test.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
layers = arcpy.mapping.ListLayers(mxd, "*", df)
for layer in layers:
    pdfname = layer.name+"PDF"
  • Presenting the whole error message will be helpful if you have any subsequent ArcPy questions. – PolyGeo Mar 28 '17 at 9:47
  • You have a confusion in using layer and map document objects. For example in your turn_on def, even though it gets a map document object, you are re assigning mxd to a string which does not make sense. Another example is that in zoom def you are putting a string as layer name i presume and expect arcpy to find this in the given mxd. This is possible if you use CURRENT map document in ArcMap but you need to list layers of mxd first to find out blah if you are using a path to create a map document object. – fatih_dur Mar 28 '17 at 19:28

You are trying to export to pdf file a string path to your .mxd file whereas you should be providing the MapDocument object.

zoomin = arcpy.mapping.ExportToPDF("C:\Users\TSPA standard\Desktop\gis\Test.mxd", "TESTPDF")

should be

zoomin = arcpy.mapping.ExportToPDF(mxd_object, "TESTPDF.pdf")

Look in the ExportToPDF help page

| improve this answer | |
  • hmm I'm still getting the same thing Here's the line that's highlighted in red from the code inspector: def pageLayout(self): return convertArcObjectToPythonObject(self._mxd._arc_object.pageLayout) – Luca E Mar 28 '17 at 10:55
  • Install PyScripter and run code in Debugging mode. Execute your code line by line to see which line it breaks and then update your question. sourceforge.net/projects/pyscripter/files/… – Alex Tereshenkov Mar 28 '17 at 11:46

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.