Select to view content in your preferred language

Export to PDF Data Driven Pages

4176
5
02-06-2014 01:30 PM
MichaelDeseo
Emerging Contributor
Hi, I'm new to Python and trying to write a script that will do the following:

1.  Loop through all mxd's in a folder and edit a Text Element named "SubTitle"
2.  Save the mxd
3.  Export each mxd to pdf

The problem is there are some mxd's with Data Driven Pages enabled, so I can't get the script to export ALL pages of a multi-page map.  I wrote an "if-else" so that I can distinguish the multi-page maps from the single page maps.  Everything else works fine except for the ddp map. 

The error msg I get:
ddp.ExportToPDF(mxd, basename, "ALL")
AttributeError: 'DataDrivenPages' object has no attribute 'ExportToPDF'

See attached screenshot of the code.


[ATTACH=CONFIG]31200[/ATTACH]
Tags (2)
0 Kudos
5 Replies
ChrisPedrezuela
Frequent Contributor
try ddp.exportToPDF

Hi, I'm new to Python and trying to write a script that will do the following:

1.  Loop through all mxd's in a folder and edit a Text Element named "SubTitle"
2.  Save the mxd
3.  Export each mxd to pdf

The problem is there are some mxd's with Data Driven Pages enabled, so I can't get the script to export ALL pages of a multi-page map.  I wrote an "if-else" so that I can distinguish the multi-page maps from the single page maps.  Everything else works fine except for the ddp map. 

The error msg I get:
ddp.ExportToPDF(mxd, basename, "ALL")
AttributeError: 'DataDrivenPages' object has no attribute 'ExportToPDF'

See attached screenshot of the code.


[ATTACH=CONFIG]31200[/ATTACH]
0 Kudos
MichaelDeseo
Emerging Contributor
Tried ddp.exportToPDF but no luck
0 Kudos
ChrisPedrezuela
Frequent Contributor
Did you use the correct inputs for that export function?

This small bit works actually,

if mxd.isDDPEnabled:
    ddp = mxd.dataDrivenPages
    ddp.exportToPDF(r'E:\test.pdf','ALL')



I have sample export code I did for my mxd's that are mixed DDP and normal ones:

for mxdDoc in mapList:
    mxd = arcpy.mapping.MapDocument(mxdDoc)
    base = os.path.splitext(os.path.basename(mxdDoc))[0]

    print 'Checking if DDP'
    if mxd.isDDPEnabled == False:
        pdfName = os.path.join(outPath, base +'.pdf')
        arcpy.mapping.ExportToPDF(mxd, pdfName, '', resolution=300, image_compression='ADAPTIVE', layers_attributes='NONE', image_quality='BEST')
    else:
        if mxd.dataDrivenPages.pageCount <= 3:
            for pageName in pageNames:
                mxd.dataDrivenPages.refresh()
                pageID = mxd.dataDrivenPages.getPageIDFromName(pageName)
                mxd.dataDrivenPages.currentPageID = pageID
                pdfName = os.path.join(outPath, base +'_'+str(pageName)+'.pdf')
                mxd.dataDrivenPages.exportToPDF(pdfName, 'CURRENT', '', 'PDF_SINGLE_FILE', 300, 'BEST', 'RGB',True, 'ADAPTIVE', 'RASTERIZE_PICTURE', False, True)
        del mxdDoc, mapList
del mxd


Also you don't need to do "for" statement to get your text element since there is a wildcard.

Just try,
x = arcpy.mapping.ListLayoutElements(mxd, TextElement, 'wildcard')[0]
0 Kudos
MichaelDeseo
Emerging Contributor
Thanks for your help, I was able to get the code to work.  I modified my inputs and you were originally correct in using 'ddp.exportToPDF' with the lower case 'e.' I had also changed to lower case 'e' in the else portion with the arcpy.mapping module but then kept getting an error there.  I guess there is a difference between 'exportToPDF' and 'ExportToPDF' when using dpp and arcpy.mapping, which is pretty confusing.  I also removed the 'for' statement like you said since it wasn't needed. 

Below is the final code.  Thanks again!


import arcpy, os
from arcpy import env


Workspace = r"C:\Users\mdeseo\Desktop\PYTHON_TEST"
arcpy.env.workspace = Workspace
mxdList = arcpy.ListFiles("*.mxd")
NewSubTitle = "THIS IS THE NEW SUBTITLE"

for mapdoc in mxdList:
    filePath = os.path.join(Workspace, mapdoc)
    mxd = arcpy.mapping.MapDocument(filePath)
    if mxd.isDDPEnabled:
        ddp = mxd.dataDrivenPages
        basename = filePath
        x = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT", "SubTitle")[0]
        x.text = NewSubTitle
        mxd.save()
        ddp.exportToPDF(basename, "ALL")
        del mxd
    else:
        x = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT", "SubTitle")[0]
        x.text = NewSubTitle
        basename = filePath
        mxd.save()
        arcpy.mapping.ExportToPDF(mxd, basename)
        del mxd
        
print "Complete"
0 Kudos
Tommy_KurniawanSubianto
Emerging Contributor

hi michael. can this phython code be used? I've tried but eror

0 Kudos