2

How can I write this python script to export each page in a MapSeries to a PDF, whilst using the MapSeries index layer NAME as the file name? At the moment, the script is configured to export using the page number.

Is it as simple as replacing 'ms.currentPageNumber' and 'pageNum' with 'ms.currentPageName' and 'pageNam'? And would the {0} need removing on the final line?

import arcpy, os, sys
relpath = os.path.dirname(sys.argv[0])

p = arcpy.mp.ArcGISProject("C:\\Automation\\Automation.aprx")
print ("project located")
l = p.listLayouts()[0]
print ("layouts listed")
if not l.mapSeries is None:
  ms = l.mapSeries
  if ms.enabled:
    for pageNum in range(1, ms.pageCount + 1):
      ms.currentPageNumber = pageNum
      print("Exporting page {0} of {1}".format(str(ms.currentPageNumber), str(ms.pageCount)))
      l.exportToPDF("C:\\Users\\Default\\Production\\Completed_certificates\\Exported_PDF_file_{0}".format(str(ms.currentPageNumber) + ".pdf"))
  • If there is not something more direct you could always use list comprehension with a search cursor on the index layer to write a dictionary with objectid and page name as its key value pairs. Then use the dictionary to switch page number (objectid) for page name. – PolyGeo Feb 21 '18 at 11:38
  • @PolyGeo The index layer is updated every day, with new features replacing those from the previous day. If I understand what you're suggesting, I could not write a dictionary of all map index features, as I don't know what they will be called each day. – Theo F Feb 21 '18 at 11:57
  • The dictionary would be written as part of this script using one line of code that takes a second or two to run. I don't think it being volatile will be an issue. – PolyGeo Feb 21 '18 at 12:01
  • @PolyGeo ok fair. But I'll have to research how to do that. However, is there definitely no such thing as ms.currentPageName? – Theo F Feb 21 '18 at 13:55
  • The documentation should tell you that. I have not had chance to look. – PolyGeo Feb 21 '18 at 15:56
2

You can get the value of any field in the dataset which is used to generate the map series by using the ms.pageRow.FIELD_NAME

Try this:

l.exportToPDF("C:\\Path_to_Output\\Exported_PDF_file_{0}".format(str(ms.pageRow.NAME_FIELD) + ".pdf", 300))

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.