Skip to content

[Question] How to highlight more than one instance when using set_colors #1370

Answered by CarolinaRDC
CarolinaRDC asked this question in Q&A
Discussion options

You must be logged in to vote

Found the solution. I only needed to load the page again after the update:

def highlight_within_pdf(file_path, text, page_n):
    doc = fitz.open(file_path)
    page = doc.load_page(page_n)
    text_instances = page.search_for(text)

    for inst in text_instances:
        highlight = page.add_highlight_annot(inst)
        highlight.set_colors({"stroke": (0.5, 0.8, 1)})
        highlight.update()
        page = doc.load_page(page_n)
        
    doc.save("new_pdf_highlighted.pdf", garbage=4, deflate=True, clean=True)
    doc.close

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by CarolinaRDC
Comment options

You must be logged in to vote
1 reply
@CarolinaRDC
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants