AnsweredAssumed Answered

ArcPy - How to order PDFs in PDF MapBook

Question asked by bisoftware on Apr 6, 2018
Latest reply on Apr 6, 2018 by rborchert

Hi,

The following code takes PDFs and appends them to a title page, creating a PDF MapBook. However, I can't figure out how to get the PDFs in the right order. I want the PDFs to be ordered by the month and year specified in the files (last 6 characters), rather than just alphabetical order of the whole file name. I am specifying the month using its numeric value (Example: CTDiscreteSumWithin4000_Avon_2017_5), I don't know whether than makes things easier or harder. 

 

import arcpy
import os

arcpy.env.workspace = r"C:\arcGIS_Shared\Python\Export\Discrete_CT"
path =  r"C:\arcGIS_Shared\Python\Export\Discrete_CT"

### Create the PDF MapBook file
CTpdfPath = os.path.join(path, 'Avon_DiscreteHeatMaps.pdf')
CTpdfDoc = arcpy.mp.PDFDocumentCreate(CTpdfPath)

###List all files to be added to MapBook
CTpdfList = arcpy.ListFiles("CTDiscreteSumWithin4000_Avon*")

### Add market title page to the newly created PDF document
CTpdfDoc.appendPages(os.path.join(path, "CTTitlePage.pdf"))

### Append market specific pages to the PDF
for CTpdf in CTpdfList:
    CTpdfDoc.appendPages(r"C:\arcGIS_Shared\Python\Export\Discrete_CT\%s"%CTpdf)
    print(CTpdf)
   


CTpdfDoc.saveAndClose()

Any help/advice would be appreciated. Thanks.

Outcomes