How to Automatically Merge PDF Files with Matching Names
Question details
The user needs a method to automate the merging of hundreds of PDF files stored in different directories that share the exact same base filename, including handling duplicate suffixes.

- Product
- Device & OS
- not provided
- Scenario
- Combining a large batch of PDF files grouped by identical or similar base filenames across multiple folders without manually selecting each pair.
- Observed behavior
- The user possesses multiple PDFs with matching names (e.g., '5200' and '5200' from different folders, or '5200' and '5200 (2)' in the same folder) and aims to automatically combine these pairs into single destination PDFs.
Before running any batch automation scripts, create a secure backup of all your original PDF folders to ensure you do not accidentally overwrite or lose important files during the merging process.
Automate PDF Merging Using a Python Script
Using Python with a PDF library is the most flexible and reliable way to handle custom naming rules, strip duplicate suffixes, and process hundreds of folders automatically.
By utilizing Python's PyPDF2 or PyMuPDF libraries, you can write a script that scans your directories, normalizes filenames by removing suffixes like '(2)', groups matching files together, and merges them sequentially into a designated output folder.
Open your terminal or command prompt and run 'pip install PyPDF2' to install the PDF manipulation library.
Open a code editor and write a Python script using the 'os' and 'PyPDF2' modules. Use 'os.walk()' to iterate through your source folders and collect file paths.
Use the 're' (regular expression) module to identify and strip suffixes like ' (2)' from the filenames so that '5200' and '5200 (2)' are grouped under the same base name '5200'.
For each grouped base name, use 'PdfMerger()' from PyPDF2 to append the files in your desired order, and use 'merger.write()' to save the combined PDF to a new destination folder.

Use PowerShell and PDFtk for Windows Batch Merging
For Windows users who prefer command-line execution without installing a full programming language, PowerShell can be paired with a CLI tool like PDFtk to group and merge files.
Easily Merge and Manage PDFs with WPS Office
While scripts are highly effective for automating complex naming rules across hundreds of folders, standard PDF merging can be accomplished quickly and securely using the intuitive interface of WPS Office.
- 1. Open the PDF tools in WPS Office: Launch WPS Office and navigate to the 'PDF' tab on the main dashboard.
- 2. Select the Merge PDF tool: Click on the 'Merge PDF' button located in the primary toolbar.
- 3. Add your files: Click 'Add Files' to select the PDFs you want to combine. You can drag and drop multiple files at once.
- 4. Organize and merge: Drag the files to arrange them in the correct sequence, name your output file, and click the 'Merge' button to generate your new document.

Frequently Asked Questions
How do I handle files with the same name in the same folder?
Operating systems do not allow two files with the exact same name in the same folder. They automatically append suffixes like ' (2)'. To automate the merge, your script must include logic to recognize and remove these specific suffix patterns before grouping the files by their base name.
Are there visual software tools that can auto-merge by filename without coding?
Most standard visual PDF editors offer manual batch merging but lack the specific logic to auto-match and group files by base names across different directories. For automated name-matching, specialized automation software (like AutoHotkey) or custom scripts (Python/PowerShell) are usually required.
Will merging PDFs affect the quality of the original documents?
No. When you merge PDFs using standard scripts or tools like PyPDF2, PDFtk, or WPS Office, the text and image quality remains identical to the originals. The software simply combines the existing pages into a new container without compressing or altering the visual data.




