Skip to content

Instantly share code, notes, and snippets.

@theamanbhargava
Last active January 21, 2025 10:11
Show Gist options
  • Select an option

  • Save theamanbhargava/4559cbd2394898dcd97ad6e8d5f655b2 to your computer and use it in GitHub Desktop.

Select an option

Save theamanbhargava/4559cbd2394898dcd97ad6e8d5f655b2 to your computer and use it in GitHub Desktop.
Merge PDFs
from PyPDF2 import PdfMerger
import os
def merge_pdfs(input_folder, output_filename):
"""
Recursively merge all PDFs in the specified folder and its subfolders into a single PDF file.
Args:
input_folder (str): Path to the folder containing PDF files
output_filename (str): Name of the output merged PDF file
"""
# Create a PdfMerger object
merger = PdfMerger()
# List to store all found PDF files with their full paths
pdf_files = []
# Recursively walk through directory
for root, dirs, files in os.walk(input_folder):
for file in files:
if file.lower().endswith('.pdf'):
# Get the full path of the PDF file
file_path = os.path.join(root, file)
pdf_files.append(file_path)
# Sort files to ensure consistent ordering
pdf_files.sort()
# Check if there are any PDF files
if not pdf_files:
print("No PDF files found in the specified folder and its subfolders.")
return
try:
# Append each PDF file to the merger
for pdf_path in pdf_files:
merger.append(pdf_path)
# Get relative path for cleaner output
relative_path = os.path.relpath(pdf_path, input_folder)
print(f"Added: {relative_path}")
# Write the merged PDF to the output file
merger.write(output_filename)
merger.close()
print(f"\nSuccessfully merged {len(pdf_files)} PDF files into {output_filename}")
except Exception as e:
print(f"An error occurred: {str(e)}")
finally:
merger.close()
# Example usage
if __name__ == "__main__":
# Replace these with your folder path and desired output filename
folder_path = "addition_pages/addition-pages_56-70"
output_file = f'{folder_path}.pdf'
merge_pdfs(folder_path, output_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment