3

I'm trying to generate individual mxds for each page in my data driven pages (which I have set up already). I have 38 pages in total, so I would need to generate 38 mxds. The pages are named according to the zone (A, B, C etc.) and the number assigned (1,2,3 etc). Each page would be named something like A1, B2, or C13. The zone name is recorded in attribute table under the field name "DIST_NAME".

I'm not very familiar with python, and this my first foray into python in ArcGIS. I've managed to piece together some code. I have two versions; neither of which works.

Version 1:

I was able to generate individual mxds with this version, but I didn't get the proper file names. I gave me 1, 2, 3 etc. when I wanted A1, A2, A3 etc.

for pageNum in range(1, mxd.dataDrivenPages.pageCount +1):
   mxd.dataDrivenPages.currentPageID = pageNum
   mxd.saveACopy(r"H:\Test\\" + str(pageNum) + ".mxd")
   mxd.dataDrivenPages.refresh()

Version 2:

I got the two pageID lines off from the help site. This generated 1 mxd with the incorrect name.

for pageNum in range(1, mxd.dataDrivenPages.pageCount +1):
   pageID = mxd.dataDrivenPages.getPageIDFromName("DIST_NAME")
   mxd.dataDrivenPages.currentPageID = pageID
   mxd.saveACopy(r"H:\Test\\" + str(pageID) + ".mxd")
   mxd.dataDrivenPages.refresh()

Can anyone please point out what I'm doing wrong in my code?

  • As far as I understand it, Arcpy cannot be used to create new .mxds. I'm not sure why this is but ESRI has some explanation as to why they do not include this seemingly useful functionality. You must create your mxds first. They can be empty shells, template mxds, that you load the data into, then save a copy. Maybe there is something in this thread that can help you. gis.stackexchange.com/questions/50714/… – Jeff May Nov 6 '15 at 18:24
  • Instead of making multiple .mxd, would your workflow support 38 dataframes? – Paul Nov 6 '15 at 18:30
  • Jeff - That's really strange becuase with the version 1 of my code above, I was able to generate the individual mxds with the correct content. It was just the names that were off. Thanks for taking your time to answer. – lo123 Nov 6 '15 at 18:50
  • Paul - I'm not familiar with workflows. Can you please explain further what you mean? – lo123 Nov 6 '15 at 18:50
3

The parameter you are sending to mxd.dataDrivenPages.getPageIDFromName() is incorrect.

If you use: mxd.dataDrivenPages.getPageIDFromName(str(pageNum))

I think you will get the result you are looking for. (When i ran it on my test document, with a data driven pages set of 6 polygons, I ended up with 6 MXDs, each with a different name. I think that is what you are going for.)

mxd.dataDrivenPages.getPageIDFromName() returns the ID of the record with the Name that is passed to it as an argument. (Using the fields you specified as the Name and ID when you set up the Data Driven Pages for the document initially.) You were passing a string ("DIST_NAME"), that doesn't match any record, so it's not returning a valid name. (This is what caused my initial confusion on this, I had it backwards - thinking the value from the Name field was being pulled instead of the ID.)

Then your code just created a bunch of MXDs that all have the same name - instead of one for each.

EDIT --

After some further work I got to here:

for pageNum in range(1, mxd.dataDrivenPages.pageCount +1): mxd.dataDrivenPages.currentPageID = pageNum print "Exporting page {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount)) pageName = mxd.dataDrivenPages.pageRow.getValue("Other") mxd.saveACopy("C:\\Test\\" + str(pageNum) + "_" + pageName + ".mxd") mxd.dataDrivenPages.refresh()

That exports an mxd with a name that is the current pageNum value and then the value of the field name that is passed to mxd.dataDrivenPages.pageRow.getValue("Other") - you will likely want to change "Other" to "DIST_NAME" and adjust the other bits to your preferences.

  • Kevin - Thanks for answering. However, I'm still not getting it. When I rand my version of the code, it still generated only 1 file. This is what code looks like: for pageNum in range(1, mxd.dataDrivenPages.pageCount +1): ... pageID = mxd.dataDrivenPages.getPageIDFromName(str("DIST_NAME")) ... mxd.dataDrivenPages.currentPageID = pageID ... mxd.saveACopy(r"H:\Test\\" + str(pageID) + ".mxd") ... mxd.dataDrivenPages.refresh() – lo123 Nov 6 '15 at 19:27
  • See my edit to my answer - I think I have addressed your issue. Using .pageRow.getValue() allows you to grab the value of a field for the current page. I used that to get the field name to use in the MXD file name - so that you can have names that are relevant to your project. – Kevin Nov 6 '15 at 19:38
  • Thank you very much! I was finally able to get the result that I wanted. I took your code and made the substitution like you had suggested and it worked!! – lo123 Nov 6 '15 at 19:46
  • Very glad to help out! That was an interesting problem that I've been poking at for a while on my own. Thanks for the excuse to finally figure it all out! – Kevin Nov 6 '15 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.