1 回答

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超7個(gè)贊
“找不到將這些坐標(biāo)作為輸出的方法” - 您可以通過(guò)以下方式獲取坐標(biāo):
for inst in text_instances:
print(inst)
inst是fitz.Rect包含找到的文本的左上角和右下角坐標(biāo)的對(duì)象。所有信息都可以在docs 中找到。
我設(shè)法使用以下代碼片段突出顯示點(diǎn)并保存裁剪區(qū)域。我正在使用 python 3.7.1,我的輸出fitz.version是('1.14.13', '1.14.0', '20190407064320').
import fitz
doc = fitz.open("foo.pdf")
inst_counter = 0
for pi in range(doc.pageCount):
page = doc[pi]
text = "hello"
text_instances = page.searchFor(text)
five_percent_height = (page.rect.br.y - page.rect.tl.y)*0.05
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(2, 2)
pix = page.getPixmap(matrix=zoom_mat, clip = hl_clip)
pix.writePNG(f"pg{pi}-hl{inst_counter}.png")
doc.close()
我在一個(gè)帶有“你好”的樣本 pdf 上對(duì)此進(jìn)行了測(cè)試:
腳本的一些輸出:
添加回答
舉報(bào)