How to erase text in a specific area #4086
Unanswered
xiaolibuzai-ovo
asked this question in
Q&A
Replies: 3 comments 9 replies
-
Best use redaction annotations: page.add_redact_annot(rect)
page.apply_redactions()
page.insert_htmlbox(rect,...) |
Beta Was this translation helpful? Give feedback.
2 replies
-
Have you ever looked at the documentation of |
Beta Was this translation helpful? Give feedback.
4 replies
-
Ok, look at this then: import pymupdf
doc = pymupdf.open("test.pdf")
page = doc[0]
blocks = page.get_text("dict", flags=pymupdf.TEXTFLAGS_TEXT)["blocks"]
spans = [s for b in blocks for l in b["lines"] for s in l["spans"]]
for s in spans:
page.add_redact_annot(s["bbox"])
page.apply_redactions(images=0, graphics=0, text=0)
for s in spans:
text = "".join([("x" if c != " " else " ") for c in s["text"]])
color = "%02x%02x%02x" % pymupdf.sRGB_to_rgb(s["color"])
html = f'<span style="color: {color};font-size:{s["size"]}px;">{text}</span>'
page.insert_htmlbox(s["bbox"], html)
page.clean_contents()
doc.subset_fonts() # subset the used font
# save using maximum compression
doc.ez_save("x.pdf", garbage=4) |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I want to extract text, translate it, and then paste it back, but I don't want to cover it with any color. So I would like to ask if there is an API to erase text in a specific area, or to get the background color for filling.
thanks for you help!
Beta Was this translation helpful? Give feedback.
All reactions