How to edit/modify/replace text in pdf by using python?

44 views Asked by At

Below is my code.

import fitz  # import PyMuPDF...for whatever reason it is called fitz

doc = fitz.open("C:\\Users\\user\\Desktop\\abcd.pdf")  # the file with the text you want to change
search_term = "original"

for page in doc:
    found = page.search_for(search_term)  # list of rectangles where to replace
    for item in found:
        font = page.get_fonts(item)
        page.add_redact_annot(item, '')  # create redaction for text
        page.apply_redactions()  # apply the redaction now
        page.insert_text(item.bl - (0, 3), "modified")


doc.save("C:\\Users\\user\\Desktop\\abcd.pdf")

This code is well-working but i have two questions. First, how to apply original's attribute like size or font? Second, why use (0, 3)? How about (0, 1) or (0,5)?

I want to change the text without any awkwardness. Please let me know how to apply attribute.

0

There are 0 answers