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]
if mxd.isDDPEnabled: ddp = mxd.dataDrivenPages ddp.exportToPDF(r'E:\test.pdf','ALL')
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
x = arcpy.mapping.ListLayoutElements(mxd, TextElement, 'wildcard')[0]
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"
hi michael. can this phython code be used? I've tried but eror