How to create a list of MXDs that have specific parameters?

252
4
Jump to solution
04-11-2019 05:47 AM
ShelbyZemken1
New Contributor III

I'm looking to create two lists of mxds, one where data driven pages is enabled, and one where it is not. 

I have a single directory with 15 mxds inside, where 2-3 have data driven pages enabled. Is there a way for me to create two lists of MXDs based on whether or not ddp is enabled? I couldn't get it to work but I imagine it would look something like this:

#need to loop through mxd_list and grab mxds in a directory where ddpIsEnabled. If true the put in list
mxd_list = arcpy.ListFiles("*.mxd",isddpEnabled = False)
ddp_list = arcpy.ListFiles("*.mxd'",isddpEnabled = True)
0 Kudos
1 Solution

Accepted Solutions
ShelbyZemken1
New Contributor III

Hi all,

I've figured out a working solution for this problem. Working code is below.

Thank you Marisa and Egge-Jan!

# 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 = 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(documentname) + ' DDP enabled mxd exported successfully.')
del mxd
else:
arcpy.mapping.ExportToPDF(mxd, output_name)
arcpy.AddMessage('\t' + str(documentname) + ' exported successfully.')
del mxd
arcpy.AddMessage(('Exports complete!'))
complete = os.startfile(output_dir)
complete

View solution in original post

4 Replies
Egge-JanPollé1
MVP Regular Contributor

Hi Shelby Zemken‌,

In ArcPy the MapDocument class has a property dataDrivenPages that returns a DataDrivenPages object that can then be used to manage the pages in a Data Driven Pages enabled map document.

Is this something you could use? If an empty object is returned, Data Driven Pages is most probably turned off or something like that...

Here's the documentation:

MapDocument—Help | ArcGIS Desktop 

HTH,

Egge-Jan

MarisaClaggett
Occasional Contributor II

Hi Shelby,

I played around with this idea and the way I can see someone doing it is getting a list of MXD file paths, creating Map Document objects, and then appending those objects to a list based on whether Data Drive Pages are enabled. Depending on what you plan on doing next, you may want to pull the file paths or MXD titles, and create a new list that are just unicode objects instead of MXDs. Hope this helps!

import arcpy

arcpy.env.workspace = r"C:\Users\username\Desktop"
list = arcpy.ListFiles("*.mxd")

mxdnames = []

for item in list:
... print (str(item))
... mxdnames.append(str(item))

mxds = []

for file in mxdnames:
... mxd = arcpy.mapping.MapDocument(r"{}\{}".format(arcpy.env.workspace,file))
... mxds.append(mxd)

falselist = []

for m in mxds:
... print (type(m))
... if m.isDDPEnabled == False:
...    falselist.append(m)

for map in falselist:
... print (map.filePath)

Best,

Marisa

ShelbyZemken1
New Contributor III

Thank you Marisa Claggett and Egge-Jan Pollé for your assistance.

I'm so close having this in working order. Now that the map documents are stored in lists, I can't get this to export to the output folder, only to the environment/workspace. Can someone take a look at this and maybe see where my small error is? It has to do with my output directory in the if/else loop.If I fix it one way, it breaks it in another way. I think it has something to do with how map documents are treated, but I'm still a struggling novice. 

EDIT: Does this issue potentially stem from working with unicode objects instead of MXDs? 

# 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)
output_dir = arcpy.GetParameterAsText(1)
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

0 Kudos
ShelbyZemken1
New Contributor III

Hi all,

I've figured out a working solution for this problem. Working code is below.

Thank you Marisa and Egge-Jan!

# 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 = 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(documentname) + ' DDP enabled mxd exported successfully.')
del mxd
else:
arcpy.mapping.ExportToPDF(mxd, output_name)
arcpy.AddMessage('\t' + str(documentname) + ' exported successfully.')
del mxd
arcpy.AddMessage(('Exports complete!'))
complete = os.startfile(output_dir)
complete

View solution in original post