BaseMax / PDFSmartEdit
Edit and replace text in PDFs with Python and PyMuPDF - simple, fast, and accurate text replacement that keeps the original font and style.
README
๐ PDF Smart Edit
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.
๐ Features
- ๐ 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)
๐ฆ Requirements
Before running the script, install the following dependencies:
pip install PyMuPDF
Or, if you use requirements.txt:
pip install -r requirements.txt
๐ก Usage
- 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.
๐ง Example Output
๐น 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
โ๏ธ Configuration Details
REPLACEMENTS
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},
]
Fonts
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).
๐งฐ Project Structure
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
๐งโ๐ป Author
Seyyed Ali Mohammadiyeh (Max Base)
๐ https://github.com/BaseMax
๐ License
This project is licensed under the MIT License.
๐ฌ Contributing
Contributions, issues, and feature requests are welcome!
Feel free to open an issue or a pull request.
โญ Support
If you find this project helpful, please consider giving it a โญ on GitHub!
