I'm working on creating a GP tool that simply exports all MXDs in an input folder into an output folder as PDFs. This actually works, but I messed something up with output directories. I coded something incorrectly so where both loops export to different folders, and not to the output folder. Can someone check this and see what I'm doing wrong with my output directories?
This worked well until one of the MXDs has data driven pages enabled, which exports only the first sheet. I believe this is because data driven pages can't be exported using [mapping.ExportToPDF][1], but instead with [datadrivenpages.exportToPDF.][2] To fix this, I test each MXD to see if they have an attribute 'datadrivenpages'. If yes, pass it through the ddp function, else pass it to the normal export function. I messed up my output directories, but I cant figure out how to fix it!
# Broken code here. Working code is below.
# Script takes all .mxd files in a user defined directory and
# exports them to another user defined directory
# This script accounts for map documents with data driven pages
import os, arcpy
# user sets input and output directories
input_dir = arcpy.GetParameterAsText(0)
#C:\Users\me\Documents\Projects\City Construction\mxd
output_dir = arcpy.GetParameterAsText(1)
#C:\Users\me\Documents\Projects\City Construction\pdf
arcpy.env.workspace = ws = str(input_dir)
list = arcpy.ListFiles("*.mxd")
mxdnames = []
for item in list:
print(str(item))
mxdnames.append(str(item))
print('All Mxds - ' + str(mxdnames))
for mxd in mxdnames:
input = os.path.join(ws, mxd)
mxd = arcpy.mapping.MapDocument(input)
output_name = (os.path.join(output_dir, mxd[:-4]) + ".pdf")
if hasattr(mxd, "dataDrivenPages"):
ddp = mxd.dataDrivenPages
ddp.exportToPDF(output_name, "ALL")
arcpy.AddMessage('\t' + str(mxd) + str(' exported successfully (Data Driven Pages Enabled).'))
del mxd
else:
arcpy.mapping.ExportToPDF(mxd, output_name)
arcpy.AddMessage('\t' + str(mxd) + str(' exported successfully.'))
del mxd
arcpy.AddMessage(('Exports complete!'))
complete = os.startfile(output_dir)
complete
----------
# Solved
# This script prompts a user for an input directory containing MXDs,
# and prompts for an output directory where PDFs are batch exported
# This script exports both normal MXDs and MXDs with data driven pages enabled.
import os, arcpy
# user sets input and output directories
input_dir = arcpy.GetParameterAsText(0)
output_dir = arcpy.GetParameterAsText(1)
arcpy.env.workspace = ws = str(input_dir)
mxdnames = arcpy.ListFiles("*.mxd")
for documentname in mxdnames:
input = os.path.join(ws, documentname)
mxd = arcpy.mapping.MapDocument(input)
output_name = (os.path.join(output_dir, documentname[:-4]) + ".pdf")
if hasattr(mxd, "dataDrivenPages"):
ddp = mxd.dataDrivenPages
ddp.exportToPDF(output_name, "ALL")
arcpy.AddMessage('\t' + str(mxd) + str(' exported successfully (Data Driven Pages Enabled).'))
del mxd
else:
arcpy.mapping.ExportToPDF(mxd, output_name)
arcpy.AddMessage('\t' + str(mxd) + str(' exported successfully.'))
del mxd
arcpy.AddMessage(('Exports complete!'))
complete = os.startfile(output_dir)
complete
````````````````````````````````````
[1]: https://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/exporttopdf.htm
[2]: http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-mapping/datadrivenpages-class.htm