Skip to content

Instantly share code, notes, and snippets.

@bhanuraja
Last active June 2, 2019 15:37
Show Gist options
  • Save bhanuraja/278c884fde7caafd260d9850ae98973c to your computer and use it in GitHub Desktop.
Save bhanuraja/278c884fde7caafd260d9850ae98973c to your computer and use it in GitHub Desktop.
Python script for merging PDF files using PyPDF2
import os
import re
from PyPDF2 import PdfFileMerger
loc = r"C:\Users\Bhanuraja\Downloads\Compressed\java"
merger = PdfFileMerger()
# get the list of all files in the specified directory
a = os.listdir(loc)
b = []
for a in os.listdir(loc):
if a.endswith('.pdf')&(a!='output.pdf'):
print(loc+"\\"+a)#creating absolute path for pdf file
b.append(a)# store only pdf files names on the current directory
#function for getting numbers from pdf files
def get_int(text):#return the integer in the file name
return int(re.search("\d+",str(text)).group())
b = sorted(b,key=get_int)# sorting based on the numbers in file names
for c in b:# now merging all the pdf files using merger
merger.append(loc+"\\"+c)
file = open(loc+"\\"+"output.pdf","w")#for creating file you can also manually create empty pdf file
file.close()
merger.write(loc+"\\"+"output.pdf")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment