Skip to content

Instantly share code, notes, and snippets.

@abodsakah
Created May 3, 2025 12:41
Show Gist options
  • Save abodsakah/ec420180d15d3646edf22202424f1f34 to your computer and use it in GitHub Desktop.
Save abodsakah/ec420180d15d3646edf22202424f1f34 to your computer and use it in GitHub Desktop.
A python script to merge all PDFs in a directory into one, with a title page with the file name for each pdf file
import os
from PyPDF2 import PdfMerger, PdfReader
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import LETTER
from io import BytesIO
def create_title_page(title: str) -> BytesIO:
"""Create a single-page PDF with the title text centered."""
buffer = BytesIO()
c = canvas.Canvas(buffer, pagesize=LETTER)
width, height = LETTER
c.setFont("Helvetica-Bold", 24)
c.drawCentredString(width / 2, height / 2, title)
c.showPage()
c.save()
buffer.seek(0)
return buffer
def merge_pdfs_with_titles(input_dir: str, output_path: str):
merger = PdfMerger()
# Get all PDF files in the directory, sorted by name
pdf_files = sorted(f for f in os.listdir(input_dir) if f.lower().endswith('.pdf'))
for pdf_file in pdf_files:
title = os.path.splitext(pdf_file)[0]
title_pdf = create_title_page(title)
# Add title page
merger.append(PdfReader(title_pdf))
# Add original PDF
pdf_path = os.path.join(input_dir, pdf_file)
merger.append(pdf_path)
# Write to output file
with open(output_path, 'wb') as fout:
merger.write(fout)
print(f"Merged PDF saved as: {output_path}")
# Example usage
if __name__ == "__main__":
input_directory = "." # Change this to your actual folder path
output_file = "merged_output.pdf"
merge_pdfs_with_titles(input_directory, output_file)
@abodsakah
Copy link
Author

You need to start by installing required libraries:

pip install PyPDF2 reportlab

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment