I am trying to figure out how to automatically merge pdf files based on a stringpart (*wildcard) in the filename, without loosing selectable layers within a pdf.

I created 3 series of layout pages with the data driven pages tool. All series include the same index features but there are different layers/raster datasets in the background (topics are: landuse, climate change, site preservation). For each topic and index feature I have already exported several multiple pdfs which include the topic and index page name. Now I would like to merge the pdfs for each feature, like:

landuse_feature1.pdf + climatechange_feature1.pdf + sitepreservation_feature1.pdf = map_feature1.pdf

landuse_feature2.pdf + climatechange_feature2.pdf + sitepreservation_feature2.pdf =


landuse_(*wildcard).pdf + climatechange_(*wildcard).pdf + 
sitepreservation_(*wildcard).pdf = map_(*wildcard).pdf

After a lot of research I found a free tool in the internet called "pdftk" which seemed to work just fine, but after merging the pdfs all selectable layers/layer informations were gone.

So, I know that there is a way to append single pdfs to a multipage pdf with arcpy, but since I have more than thousands of pdfs to merge, this is not an option for me.

Does maybe anybody had to struggle with this problem before and knows if and how it is possible to use a wildcard for merging pdfs with arcpy?


1 Answer 1


So your problem looks two-fold. First part is you want to look for a way to do searching on file names with wildcards.

I have an outline that I would follow, but I am assuming all your PDFs are in a centralized directory.

First.. Get all the file names of the PDFs. You can use something like os.listdir() and then filter out the subdirectories (if they exist) or there are a few other options. This post shows a few different ways to get file names. Either way, you need this to end with a list of the file names you have.

Second.. After you determine your wildcard / search pattern, you need to figure out a way to find the appropriate file names from your list made in the first step. Since you should have a list of file names as string, you need to do some sort of string search. Python uses a couple of different methods. The two options I found deal with regular expressions or the fnmatch module. There are more details here for using them..

Third.. Assuming have a filtered list of file names based off the string file name searching done in the second step, you should now have a list of file names you want to merge together. Using arcpy (mapping module, PDFDocumentCreate), you should be able to iterate through your list and create your larger PDF.

You should be able to import the os and other modules from python that you'd need into an arcpy script or into the interactive window. I think I have given you enough info to get you started in the right direction without writing out the whole thing. You should be able to adapt this process to fit your needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.