append pages to a pdf

4712
4
02-11-2011 11:47 AM
ChadKeith
New Contributor
Hi
I'm trying to figure out if I can append a bunch of pdfs into a single pdf document.

So I can append the files if I spell out the path and file name of each pdf using pdfDoc.appendPages.
I can also list the pdf files that I have in a folder arcpy.ListFiles("*.pdf").
 
Now if I could just combine the two functions - so that I'm listing all the pdfs in a folder and then appending them to a new single pdf.

Thanks in advance.
chad
Tags (2)
0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus
did you try
out_file.appendPages(tmp_pdf)
where out_file is an existing pdf and tmp_pdf is another one
0 Kudos
ChadKeith
New Contributor
Hi-
I'm not sure how that would help.  I'm probably just not following your logic but here is what I'm try to do with some examples:
It is simple I think - just combine listing a bunch of pdfs from a folder and appending those pdfs in the list to a new single pdf. 

Here is the first part:

import arcpy

# Set the workspace.
arcpy.env.workspace = r"C:\output"

# List all of the pdf files in the output folder.
# pdfList is a Python List returned from the ListFiles function.
pdfList = arcpy.ListFiles("*.pdf")

# Iterate through the fields and print the name of each
# field to the Interactive Window.
for pdf in pdfList:
    print pdf

and here is the second part:

import arcpy, os

#Set file name and remove if it already exists
pdfPath = r"C:\output\TimeSeries.pdf"
if os.path.exists(pdfPath):
    os.remove(pdfPath)

#Create the file and append pages
pdfDoc = arcpy.mapping.PDFDocumentCreate(pdfPath)
pdfDoc.appendPages(r"C:\output\1991.pdf")
pdfDoc.appendPages(r"C:\output\1992.pdf")
pdfDoc.appendPages(r"C:\output\1993.pdf")
pdfDoc.appendPages(r"C:\output\1994.pdf")
pdfDoc.appendPages(r"C:\output\1995.pdf")

#Commit changes and delete variable reference
pdfDoc.saveAndClose()
del pdfDoc
0 Kudos
ChrisMathers
Occasional Contributor III
well if the file names are just numbers like that you should sort the list and then just put the list into a for loop

for file in pdfList:
    pdfDoc.appendPages(r"C:\output\%s"%file)
0 Kudos
ChadKeith
New Contributor
thanks this worked perfect!
0 Kudos