Python and Data Driven Pages

2662
1
05-05-2011 12:47 PM
GrantHerbert
Occasional Contributor II
I am having real trouble exporting to PDF - dataDrivenPages.exportToPDF()

I can export fine if I use the defaults (just passing through a reference to the out_pdf) but as soon as I start adding parameters it fails, whether I try and pass them through or type them.


mxdMain = arcpy.mapping.MapDocument(r"C:\Main.mxd")
pdfMainOutput1 = r"C:\ResMapMain1.pdf"
pageCount = mxdMain.dataDrivenPages.pageCount
pageStart = pageCount - 1
outputToEnd = '"' + str(pageStart) + '-' + str(pageCount) + '"'

pdfMain = mxdMain.dataDrivenPages.exportToPDF(pdfMainOutput1, page_range_type="RANGE", page_range_string=outputToEnd, multiple_files="PDF_SINGLE_FILE", resolution=200, layers_attributes="NONE", georef_info="FALSE")



results in:

Traceback (most recent call last):
File "C:\Projects\TestDDP_params.py", line 36, in <module>
pdfMain = mxdMain.dataDrivenPages.exportToPDF(pdfMainOutput1, page_range_type="RANGE", page_range_string=outputToEnd, multiple_files="PDF_SINGLE_FILE", resolution=200, layers_attributes="NONE", georef_info="FALSE")
File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\utils.py", line 181, in fn_
return fn(*args, **kw)
File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\_mapping.py", line 400, in exportToPDF
return convertArcObjectToPythonObject(self._arc_object.exportToPDF(*gp_fixargs((out_pdf, page_range_type, page_range_string, multiple_files, resolution, image_quality, colorspace, compress_vectors, image_compression, picture_symbol, convert_markers, embed_fonts, layers_attributes, georef_info, jpeg_compression_quality), True)))
AttributeError: PageLayoutObject: Error in exporting pages

I thought it mght have been the passing of the outputToEnd reference in the page_range parameter, but it still fails with the same error when I tried the following:

pdfMain = mxdMain.dataDrivenPages.exportToPDF(pdfMainOutput1, page_range_type="RANGE", page_range_string="79-80", multiple_files="PDF_SINGLE_FILE", resolution=200, layers_attributes="NONE", georef_info="FALSE")


I am pretty new to Python so have I made a simple error or is there something with the dataDrivenPages that doesn't like parameters like this? I don't have the same problem with the standard arcpy.mapping.exportToPDF running on a non-data driven pages mxd -

pdfTitle = arcpy.mapping.ExportToPDF(mxdCover, pdfTitle, resolution=200,layers_attributes="NONE", georef_info="FALSE")


works fine
Tags (2)
0 Kudos
1 Reply
GrantHerbert
Occasional Contributor II
Update:

After a reboot the code worked fine with a hardcoded range.

The exportToPDF() will accept a variable string or a python range()*

 pageCt = mxdMain.dataDrivenPages.pageCount #=80
    print pageCt
    #outputToEnd = '"' + str(pageCount-2) + '-' + str(pageCount-1) + '"' #doesn't work
    #outputToEnd = str(pageCount-2) + '-' + str(pageCount-1) #doesn't work
    outputToEnd = range(pageCt-2, pageCt+1)  #works, but won't print first in range
    #outputToEnd = range(0, pageCt-77)  #works
    #outputToEnd = "1-2" #works
    print "outputToEnd = " + str(outputToEnd)


*but if you use a Python range it won't print the first or the last in the range (ie [7,9] will only print map 8). The last is expected (python range(7,9) = [7, 8] but skipping the first one was a surprise to me.

then this will work fine:

mxdMain.dataDrivenPages.exportToPDF(pdfMainOutput1, page_range_type="RANGE", page_range_string=outputToEnd, multiple_files="PDF_SINGLE_FILE", resolution=200, layers_attributes="NONE", georef_info="FALSE")
0 Kudos