A lightweight Python tool to search and replace text in PDF files using PyMuPDF (fitz).
It allows you to programmatically replace specific text strings while preserving the original font and size as much as possible.
- 🔍 Find and replace text inside PDF files
- 🧠 Automatically detects and reuses font and text size
- 🧹 Cleans up and redraws replaced text neatly
- ⚙️ Supports font aliasing (e.g. Arial→helv)
- 📄 Easy configuration for multiple replacements
- ✅ Works fully offline (no Adobe or APIs required)
Before running the script, install the following dependencies:
pip install PyMuPDFOr, if you use requirements.txt:
pip install -r requirements.txt- Place your source PDF in the same directory (e.g. input.pdf).
- Edit the replacement rules inside script.py:
REPLACEMENTS = [
    {"old_text": "11/10/2025", "new_text": "22/10/2025", "max_replace": 1},
    {"old_text": "10/10/2025", "new_text": "22/10/2025", "max_replace": 1},
]- Run the script:
python script.py- The edited PDF will be saved as output.pdf.
🔹 Processing page 1...
   ✅ '11/10/2025' → '22/10/2025' (1x, font=helv, size=11.0)
   ✅ '10/10/2025' → '22/10/2025' (1x, font=helv, size=11.0)
🎉 Done! Saved edited PDF → output.pdf
A list of dictionaries, each defining:
- old_text: The text to search for.
- new_text: The text to replace it with.
- max_replace: (optional) Maximum number of occurrences to replace per page.
Example:
REPLACEMENTS = [
    {"old_text": "Draft", "new_text": "Final", "max_replace": 2},
]The script automatically detects the font used near the target text. It includes built-in font aliases to ensure compatibility:
| Original Font | Internal Alias | 
|---|---|
| Arial, Calibri, Helvetica | helv | 
| Times-Roman, TimesNewRoman | times | 
| Courier | cour | 
If an unknown font is detected, it defaults to helv (Helvetica).
edit-pdf-online/
├── script.py        # Main Python script
├── input.pdf        # Example input file (you provide)
├── output.pdf       # Generated after running the script
├── README.md        # Project documentation
├── LICENSE          # MIT License
└── .gitignore
Seyyed Ali Mohammadiyeh (Max Base) 🌐 https://github.com/BaseMax
This project is licensed under the MIT License.
Contributions, issues, and feature requests are welcome! Feel free to open an issue or a pull request.
If you find this project helpful, please consider giving it a ⭐ on GitHub!