ArcPy - How to order PDFs in PDF MapBook

172
1
04-06-2018 06:11 AM
Business_IntelligenceSoftware
Occasional Contributor

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.

Tags (2)
0 Kudos
1 Reply
RobertBorchert
Frequent Contributor III

You may be working much to hard on this. 

Select your PDF's in Windows Explorer.

Right click to combine all the selected PDF's

Then in the Combine Files dialog click on the Switch to List View. Then sort by the modified field.