You can append and merge multiple PDF files in Python using the PdfWriter class from the modern pypdf (formerly PyPDF2) library.
While older tutorials might reference outdated classes like PdfFileMerger or PdfFileWriter, modern implementations unify these operations under PdfWriter. Below is a complete guide to appending PDFs using the current industry standard. Prerequisites
First, ensure you install the library. Note that the package was officially renamed from PyPDF2 to pypdf, but the core syntax remains highly compatible. pip install pypdf Use code with caution. Method 1: Appending Entire PDF Files
This approach sequentially combines entire PDF files together, adding the second file to the very end of the first file.
from pypdf import PdfWriter def append_pdfs(pdf_list, output_filename): # Initialize the PDF writer object writer = PdfWriter() # Loop through each PDF file and append it to the writer for pdf in pdf_list: writer.append(pdf) # Write the combined pages into a new output file with open(output_filename, “wb”) as output_file: writer.write(output_file) # Close the writer object to free system resources writer.close() # List of files to append in order files_to_join = [“first_document.pdf”, “second_document.pdf”, “third_document.pdf”] append_pdfs(files_to_join, “final_combined.pdf”) print(“PDFs appended successfully!”) Use code with caution. Method 2: Appending Specific Pages from a PDF
If you only need to append a specific page range from a source file to your target document, pass a tuple representing the (start, stop) page indices into the pages argument.
from pypdf import PdfWriter writer = PdfWriter() # Append the entire first document writer.append(“document1.pdf”) # Append only pages 1, 2, and 3 (index 0 to 3, exclusive of 3) from the second document writer.append(“document2.pdf”, pages=(0, 3)) # Write out the final file with open(“selective_output.pdf”, “wb”) as f: writer.write(f) writer.close() Use code with caution. Method 3: Automatically Appending All PDFs in a Directory
To quickly stitch together every PDF inside a folder, combine PdfWriter with Python’s built-in glob or os modules to dynamically discover files.
import glob from pypdf import PdfWriter def batch_append_folder(folder_path, output_name): writer = PdfWriter() # Find all files ending in .pdf within the target directory search_path = f”{folder_path}/*.pdf” for file in sorted(glob.glob(search_path)): writer.append(file) print(f”Appended: {file}“) with open(output_name, “wb”) as f: writer.write(f) writer.close() # Example usage batch_append_folder(“./my_pdf_directory”, “all_combined.pdf”) Use code with caution. Summary of Key Commands Code Syntax Initialize Merger writer = PdfWriter() Serves as the virtual canvas for the final document. Append Full File writer.append(“file.pdf”) Adds all pages from the target to the end of the sequence. Append Part of File writer.append(“file.pdf”, pages=(0,2)) Uses a zero-indexed tuple slice (start, end). Save Results writer.write(“output.pdf”) Exports the final combined PDF payload to your local drive. Resource Cleanup writer.close() Frees up system RAM and file descriptors.
If you are dealing with special file constraints, let me know. I can help you with handling password-protected PDFs, inserting pages into the middle of a document, or rotating pages before appending. AI responses may include mistakes. Learn more Stack Overflow Merge PDF Files using python PyPDF2 – Stack Overflow