__author__ = 'Jeff.xie'
import fitz


sourcefile="D:\\Project\\xxx.pdf";

# inst是fitz.Rect包含找到的文本片段的左上角和右下角坐标的对象。所有信息都可以在docs中找到。
# 我设法使用下面的代码片段突出显示点并保存裁剪区域。我正在使用python 3.7.1,我的输出fitz.version是('1.14.13', '1.14.0', '20190407064320')。

doc = fitz.open(sourcefile)
inst_counter = 0
for pi in range(doc.pageCount):
    page = doc[pi]

    # text = "FT21106F7PGQ"
    text = "FT21097G4TGP"
    text_instances = page.searchFor(text)

    # 数字越小,截取的高度越小
    five_percent_height = (page.rect.br.y - page.rect.tl.y)*0.02

    for inst in text_instances:
        inst_counter += 1
        highlight = page.addHighlightAnnot(inst)

        # define a suitable cropping box which spans the whole page
        # and adds padding around the highlighted text
        tl_pt = fitz.Point(page.rect.tl.x, max(page.rect.tl.y, inst.tl.y - five_percent_height))
        br_pt = fitz.Point(page.rect.br.x, min(page.rect.br.y, inst.br.y + five_percent_height))
        hl_clip = fitz.Rect(tl_pt, br_pt)

        zoom_mat = fitz.Matrix(4, 4)   #数字越大,截取图片的清晰度越高
        pix = page.getPixmap(matrix=zoom_mat, clip = hl_clip)
        # pix.writePNG(f"pg{pi}-hl{inst_counter}.png")
        pix.writePNG(r"D:\\shot55.png")