Select to view content in your preferred language

Combine PDFs for facing pages

1519
5
04-04-2011 01:56 PM
TomMagdaleno
Frequent Contributor
I have been furiously trying to make this code work
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s90000002p000000.htm

I think my problem is with the page numbering.  But I'm past that, I just want to know if their is a way in Python to combine two folders of PDFs.  Each folder has 68 individual PDFs, Folder1 is the left side, Folder2 is the right. 

For example...

Folder1              Folder 2
B7.pdf                C7.pdf
B8.pdf                C8.pdf
...etc                 ...etc

Is their a way to combine these two folders full of PDFs into one 136 page PDF with left and right pages based on which folder they reside in?  I'd rather not spell out every single path name and append, it defeats the purpose.
Tags (2)
0 Kudos
5 Replies
LoganPugh
Frequent Contributor
Just read the contents of both directories into a list of (PageA,PageB) pairs and iterate over the list, calling the appendPages method on PageA and then PageB.
0 Kudos
TomMagdaleno
Frequent Contributor
Thanks Logan,
  Could you give an example?  I'm new to this.
0 Kudos
TomMagdaleno
Frequent Contributor
I know I can run something like this below, but how do I create my list?  Would it have full paths?

# Author:  ESRI
# Date:    July 5, 2010
# Version: ArcGIS 10.0
# Purpose: This script will append multiple PDFs into a single output PDF file.
#          The script is intended to run within a script tool.  There are two
#          parameters:
#               1) Select PDF Files to Append,
#               2) Output PDF.
#
#Notes: The order of the PDFs is based on how they are entered.  The PDF at the
#       top of the list is first followed by those below it.

import arcpy, os, string

#Read input parameters from script tool
PDFList = string.split(arcpy.GetParameterAsText(0), ";")
outPDFpath = arcpy.GetParameterAsText(1)

#Create a new PDF object to store the results
outputPDF = arcpy.mapping.PDFDocumentCreate(outPDFpath)

#Loop through and append each PDF in the list
for eachPDF in PDFList:
    outputPDF.appendPages(str(eachPDF))

#Save the changes and open the result automatically  
outputPDF.saveAndClose()
os.startfile(outPDFpath)

#Remove variable reference to file
del outputPDF
0 Kudos
DanPatterson_Retired
MVP Emeritus
To demonstrate how to combine the lists (which may be of different lengths) see the following example:
aList = ["a", "c", "e", "g"]
bList = ["b", "d", "f", "h", "i", "j"]
n1 = len(aList)
n2 = len(bList)
combo = []
for i in range(0, n1):
  combo += [aList, bList]
if n2 > n1:
  combo += bList[n1:n2]
print combo

Ensure that aList is the shorter of the two and bList is equal to or greater than aList.
0 Kudos
PaulCone
Deactivated User
I started with that script a few months ago (I specifically upgraded to 10 so I could do this -- the rest of the City is at 9.3.1 or earlier).  I've since modified it quite a bit, but the basic idea is the same.  Don't get lost worrying about creating lists -- you're creating pages and then merging them all together.  What you're basically doing is a loop to create all the even-numbered pages, then loop to create all the odd-numbered pages, then a third loop to merge them all.  The trick is in the numbering of the pages as you create them.  First loop is 1, 3, 5, 7, ..., second loop is 2, 4, 6, 8, ..., third loop is 1, 2, 3, 4, 5, 6, 7, 8...  Keep the files names simple like the example script.  Spell out the path name completely and just loop the page number and concatenate.

Paul

---------------------------------------------------------
Paul Cone
Mapping and GIS
City of Portland, Bureau of Transportation
0 Kudos