我正在使用PyMuPDF,并试图循环遍历字符串列表,并在获取图像并移到下一个字符串之前突出显示它们。
下面的代码完成了我所需要的,但是注释在每个循环之后仍然存在,我想在图像被获取之后删除它们。
下面的示例图像显示了突出显示的“命令”一词,但前面的字符串“图片”和“文件名”仍被高亮显示,因为我将把数百个这些图像汇编到一个报告中,我想使它更加突出。
有类似于page.remove(突出显示)的东西吗?

for pi in range(doc.pageCount):
page = doc[pi]
for tag in text_list:
text = tag
text_instances = page.searchFor(text)
five_percent_height = (page.rect.br.y - page.rect.tl.y)*0.05
five_percent_width = (page.rect.br.x - page.rect.tl.x)*0.05
for inst in text_instances:
inst_counter += 1
highlight = page.addSquigglyAnnot(inst)
tl_pt = fitz.Point(max(page.rect.tl.x, inst.tl.x - five_percent_width), max(page.rect.tl.y, inst.tl.y - five_percent_height))
br_pt = fitz.Point(min(page.rect.br.x, inst.br.x + five_percent_width), 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)
>I want to remove the annotation here发布于 2020-05-25 02:54:04
我发现一个可以接受的解决方案就是在截图后将不透明度设置为0%。
pix = page.getPixmap(matrix=zoom_mat, clip = hl_clip)
highlight.setOpacity(0)
highlight.update()发布于 2020-06-11 13:46:54
这样做:
annot = page.firstAnnot
while annot:
annot = page.delete_annot(annot)该方法在删除的注释之后传递注释。
发布于 2021-09-30 07:38:52
乔尔的方法很好。然而,从文件来看,还有其他选择:
https://pymupdf.readthedocs.io/en/latest/faq.html#how-to-read-and-update-pdf-objects
此方法还可以通过将其值设置为null从xref字典中删除密钥:下面将从页面:
doc.xref_set_key(page.xref, "Rotate", "null")中删除旋转规范。同样,要从页面中删除所有链接、注释和字段,请使用doc.xref_set_key(page.xref, "Annots", "null")。由于Annots定义为数组,因此在本例中,使用doc.xref_set_key(page.xref, "Annots", "[]")语句设置en空数组将完成相同的工作。
https://stackoverflow.com/questions/61947880
复制相似问题