2

I realize that I've asked two similar questions recently, but following suggestions in the comment chain, I'll make this a separate question (separate as it is not the no output problem in ArcPy: (SearchCursor/export to tiff ) with no output or the produces empty feature problem described Arcpy: SQL error in select_analysis produces empty feature). To be clear, the question is explicitly stated as: How to print a .pdf layout for each record that meets a where clause.

Based on suggestions from @PolyGeo, I'm trying the approach of using a definitionQuery to get the values and then a combination of a search cursor with a panToExtent to print each record. Here is the code:

import arcpy

mxd = arcpy.mapping.MapDocument("C:\\temp\\Graffiti_DM1.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
outpdf="C:\\temp\\1003_zip4.pdf"
where_clause='"Incident_Z" = ' + "'10033'"
graffitidata="C:\\temp\\itscomplicated.shp"

cursor = arcpy.UpdateCursor(graffitidata)

graffitilyr=arcpy.mapping.ListLayers(mxd, "itscomplicated", df)[0]
graffitilyr.definitionQuery=where_clause
for cur in cursor:
    cur.rotation = "0"
    cur.scale = "1000"
    outFile = expdir + "\\" + cur.name + ".pdf"
    cur.panToExtent(graffitilyr.getSelectedExtent())
    arcpy.mapping.ExportToPDF(mxd,outpdf)


del cursor

However, I'm running into the following problem.

Traceback (most recent call last):
  File "C:\temp\defquery_for_in.py", line 15, in <module>
    cur.rotation = "0"
  File "C:\Program Files\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\_base.py", line 35, in __setattr__
    return setattr(self._arc_object, attr, ao)
RuntimeError: ERROR 999999: Error executing function.

If anyone has any suggestions concerning the above error, or suggestions concerning the general setup of the code (specifically the addition of the search cursor within the where clause) I'd be greatly appreciateive.

This is the current version of what I'm working with as suggested by @PolyGeo, however it is producing pdfs that have the layout but no data (see comment below).

import arcpy
mxd = arcpy.mapping.MapDocument("C:\\temp\\Graffiti_DM1.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
graffitilyr=arcpy.mapping.ListLayers(mxd, "itscomplicated", df)[0]
where_clause = '"Incident_Z" = ' + "'10003'"
graffitilyr.definitionQuery=where_clause
features = arcpy.SearchCursor(graffitilyr)
count = 1


for feature in features:
        df.rotation = "0"
        df.extent = graffitilyr.getSelectedExtent(True)
        df.scale = "1000"
        outpdf="C:\\temp\\" + str(count) + ".pdf"
        arcpy.mapping.ExportToPDF(mxd,outpdf)
        count = count+1

del features
3
  • 1
    Have you looked into using Data Driven Pages for this?
    – Fezter
    Commented Apr 22, 2013 at 2:02
  • @Fezter, I will now
    – David Meek
    Commented Apr 22, 2013 at 2:29
  • 1
    David - are 'rotation' and 'scale' fields in your dataset? By your use of cur.panToExtent it seems you are confusing your row object with your dataframe. Use .panToExtent on your dataframe, and your dF also has .rotation and .scale params. If you don't have a rotation field, (and if you do it's probably a # and not a str, this is why you are crashing.
    – Tom
    Commented Apr 22, 2013 at 4:14

1 Answer 1

1

I think the following (untested) code will do very close to what you are asking.

It assumes that you have a layer in the first data frame of your MXD called "itscomplicated".

If it "just works" without errors but needs modifications to try and tweak it to precisely what you need be sure to keep backups that you can revert to as you modify then test each part at a time.

import arcpy

mxd = arcpy.mapping.MapDocument("C:\\temp\\Graffiti_DM1.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
graffitilyr=arcpy.mapping.ListLayers(mxd, "itscomplicated", df)[0]
where_clause = '"Incident_Z" = ' + "'10033'"  # Assume this will select multiple polygons  
graffitilyr.definitionQuery=where_clause
features = arcpy.SearchCursor(graffitilyr)
count = 1
for feature in features:
    df.rotation = "0"
    outFile = expdir + "\\" + cur.name + ".pdf"
    df.extent = graffitilyr.getSelectedExtent(False)
    df.scale = "1000"
    outpdf="C:\\temp\\pdf" + str(count) + ".pdf"
    arcpy.mapping.ExportToPDF(mxd,outpdf)
    count = count+1
2
  • a slightly modified version of this runs correctly, in that it produces .pdfs, but they are all blank in terms of data, the rest of the layout is printed, i.e. title, scale bar etc, everything in the layout view but the data! Interestingly, and tellingly, it does print the right number of pdfs, i.e. 22 for the where_clause, which seems to indicate that it is correctly cycling through the records, but somehow not getting the extent correct. I will add the modified code into the original post.
    – David Meek
    Commented Apr 22, 2013 at 14:55
  • Have you tried commenting out the "df.scale"? It may be that your coordinate system is geographic rather than projected and that you are drawing too small to see. Perhaps also double-check that you have layers turned on in C:\\temp\\Graffiti_DM1.mxd as it was last saved.
    – PolyGeo
    Commented Apr 26, 2013 at 10:13

Your Answer

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

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