-2

A PDF contains information as follows:

      Name: ABC
Profession: XYZ
   Hobbies: HIJ 

Using PyMUPDF i am able to search a word in a PDF and highlight it, but i want to highlight the right hand side value of the searched text.
In this example, i want to highlight the value of Name i.e. ABC. because for a different file the Name value may change from ABC to PQR.

Can anyone help on how to do it? Thanks a lot!

Lav Mehta
  • 92
  • 1
  • 2
  • 13

1 Answers1

-1

I think i myself found a solution to this,

import fitz                           #import dependencies
doc  = fitz.open('filename.pdf')      #openPDF 
page = doc[0]

text = "Name"                         #Text intended to search
rl   = page.searchFor(text)           #coordinates of the searched text saved as list (rl-rectangle)

rect = rl[0]+(20,0,20,0)              #to move the rectangle 20 pixels to the right
highlight = page.addHighlightAnnot(rect) # highlight

doc.save("output.pdf", garbage=4, deflate=True, clean=True)

Number of Pixels has to be approximated or set after few trials depending on which direction and how far to move the rectangle. Hope this helps!

Lav Mehta
  • 92
  • 1
  • 2
  • 13