Skip to content

Instantly share code, notes, and snippets.

@dijikul
Last active March 19, 2025 00:20
Show Gist options
  • Save dijikul/cb017ed242a2866c175582bbdb960343 to your computer and use it in GitHub Desktop.
Save dijikul/cb017ed242a2866c175582bbdb960343 to your computer and use it in GitHub Desktop.
Combine Multiple PDF's into One (JFK File Analysis)
import os
import PyPDF2
def merge_pdfs(input_directory, output_file):
# Create a PDF merger object
merger = PyPDF2.PdfMerger()
# Get a sorted list of all PDF files in the directory
pdf_files = sorted([f for f in os.listdir(input_directory) if f.lower().endswith('.pdf')])
if not pdf_files:
print("No PDF files found in the directory.")
return
print(f"Merging {len(pdf_files)} PDF files...")
# Loop through and append each PDF
for pdf in pdf_files:
pdf_path = os.path.join(input_directory, pdf)
try:
merger.append(pdf_path)
print(f"Added: {pdf}")
except Exception as e:
print(f"Error adding {pdf}: {e}")
# Write the final merged PDF
merger.write(output_file)
merger.close()
print(f"Successfully created merged PDF: {output_file}")
if __name__ == "__main__":
input_directory = "path/to/your/pdf/directory" # Change this to your directory
output_file = "merged_output.pdf" # Change to your preferred output filename
merge_pdfs(input_directory, output_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment