Snippet Name: Merge_PDFs_PowerShell_Python
Tags:
Created Date: 2025-06-16 13:12:45
Last Modified Date: 2025-05-31 10:32:09
Dependencies:
DBA Notes:
Sample Output:
Title: Merge PDFs PowerShell Python
Author: user
i have a folder full of pdfs located at D:\pdf. they are named in order, like "scan00007.pdf" and increment, help me merge the pdfs file sinto a single document
Author: ChatGPT
You can merge all the PDFs in `D:\pdf` into a single document using PowerShell and the `PdfSharp` .NET library or Python with `PyMuPDF` (`fitz`). Below are two methods:
---
### **Method 1: PowerShell with PdfSharp**
1. Install the `PdfSharp` library if you don???t have it:
- Download `PdfSharp.dll` from [NuGet](https://www.nuget.org/packages/PdfSharp/)
- Extract it to `D:\pdf\PdfSharp.dll`
2. Run the following PowerShell script:
```powershell
Add-Type -Path "D:\pdf\PdfSharp.dll"
$pdfFiles = Get-ChildItem -Path "D:\pdf" -Filter "*.pdf" | Sort-Object Name
$outputFile = "D:\pdf\Merged.pdf"
$pdf = New-Object PdfSharp.Pdf.PdfDocument
foreach ($file in $pdfFiles) {
$doc = [PdfSharp.Pdf.IO.PdfReader]::Open($file.FullName, [PdfSharp.Pdf.IO.PdfDocumentOpenMode]::Import)
foreach ($page in $doc.Pages) {
$pdf.AddPage($page)
}
}
$pdf.Save($outputFile)
Write-Host "Merged file saved to $outputFile"
```
---
### **Method 2: Python with PyMuPDF (`fitz`)**
If you have Python installed, install `PyMuPDF`:
```sh
pip install pymupdf
```
Then, use this script to merge the PDFs:
```python
import fitz
import os
pdf_folder = r"D:\pdf"
output_pdf = r"D:\pdf\Merged.pdf"
pdf_files = sorted([f for f in os.listdir(pdf_folder) if f.endswith(".pdf")])
merged_doc = fitz.open()
for pdf in pdf_files:
with fitz.open(os.path.join(pdf_folder, pdf)) as doc:
merged_doc.insert_pdf(doc)
merged_doc.save(output_pdf)
print(f"Merged PDF saved to {output_pdf}")
```
Let me know if you need adjustments! ????