Skip to content

Edit and replace text in PDFs with Python and PyMuPDF - simple, fast, and accurate text replacement that keeps the original font and style.

License

Notifications You must be signed in to change notification settings

BaseMax/PDFSmartEdit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

📝 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. Arialhelv)
  • 📄 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

  1. Place your source PDF in the same directory (e.g. input.pdf).
  2. 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},
]
  1. Run the script:
python script.py
  1. 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!

About

Edit and replace text in PDFs with Python and PyMuPDF - simple, fast, and accurate text replacement that keeps the original font and style.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages