This Python script merges all PDF files found within the current working directory and its subdirectories into a single PDF file named "outputfile.pdf". Here's how the script works:
- It imports necessary modules: os for file operations and PyPDF2 for working with PDF files.
- It initializes an empty list pdfFiles to store paths of PDF files found in the directory.
- It traverses through the current working directory and its subdirectories using os.walk(), and for each file found, it checks if the file has a ".pdf" extension. If it does, it adds the file's path to the pdfFiles list.
- It sorts the pdfFiles list alphabetically (case-insensitive).
- It initializes a PdfWriter object from PyPDF2.
- It iterates over each PDF file in the pdfFiles list:
- Opens the PDF file in binary read mode.
- Initializes a PdfReader object from PyPDF2.
- Iterates over each page in the PDF and adds it to the pdfWriter object.
- After iterating through all PDF files, it opens a new PDF file named "sbi.pdf" in binary write mode.
- It writes the content of the pdfWriter object (which contains all pages from the input PDF files) to the output PDF file.
Finally, it closes the output PDF file.
This script effectively merges multiple PDF files into one, consolidating their contents into a single PDF document.