python-3.x 将椭圆形状添加到PDF中突出显示的文本周围

dfuffjeb  于 2023-03-09  发布在  Python
关注(0)|答案(2)|浏览(146)
import fitz
def highlight_text(pdf_file, room_id):
        for page in pdf_file:
            ### SEARCH
            text = str(room_id)
            print(text)
            text_instances = page.search_for(text)

            ### HIGHLIGHT
            for inst in text_instances:
                highlight = page.add_highlight_annot(inst)
                highlight.set_colors({"stroke":(0, 1, 0), "thickness": 2})  # <-- change thickness here
                highlight.update()
                print(highlight)

                # create a shape object
                shape = page.new_shape()
                # draw a circle with width=5 (change it to whatever you want)
                shape.draw_circle(((inst.x0+inst.x1)/2, (inst.y0+inst.y1)/2), 70)
                shape.finish(color=(0,1,0), fill=None, width=5)
                # update the page with the drawing
                shape.commit()

            ### OUTPUT
            pdf_file.save("highlighted.pdf", garbage=4, deflate=True, clean=True)

pdf = fitz.open("OK31017-ML0100-DWG-ZA-B1.pdf")
highlight_text(pdf, "MECH PUMP ROOM (WATER FEATURE 12)")

我已经尝试用矩形来包围高亮显示的文本。现在,我想把它改成圆形,并增加笔画粗细,这样它会更明显。
我使用的是Python 3

vh0rcniy

vh0rcniy1#

找到了答案,

circle_center = fitz.Point((inst.tl.x + inst.br.x)/2, (inst.tl.y + inst.br.y)/2)
                circle_width = 300
                circle_height = 150
                circle_bbox = fitz.Rect(circle_center.x - circle_width/2, circle_center.y - circle_height/2,
                             circle_center.x + circle_width/2, circle_center.y + circle_height/2)
                circle_annot = page.add_circle_annot(circle_bbox)
                circle_annot.set_colors(stroke=(1,0,0), fill=(1, 1, 0))
                circle_annot.set_border(width=10)
                circle_annot.set_opacity(0.7)
                circle_annot.border_width = 10
                circle_annot.update() ```
p5cysglq

p5cysglq2#

可以使用Shape对象绘制圆:

def highlight_text(self, pdf_file, room_id):
        for page in pdf_file:
            ### SEARCH
            text = str(room_id)
            print(text)
            text_instances = page.search_for(text)

            ### HIGHLIGHT
            for inst in text_instances:
                highlight = page.add_highlight_annot(inst)
                highlight.set_colors({"stroke":(0, 1, 0), "thickness": 2})  # <-- change thickness here
                highlight.update()
                print(highlight)

                # create a shape object
                shape = page.new_shape()
                # draw a circle with width=5 (change it to whatever you want)
                shape.draw_circle(((inst.x0+inst.x1)/2, (inst.y0+inst.y1)/2), 70)
                shape.finish(color=(0,1,0), fill=None, width=5)
                # update the page with the drawing
                shape.commit()

            ### OUTPUT
            pdf_file.save("highlighted.pdf", garbage=4, deflate=True, clean=True)

以上是下面代码的结果:

import fitz
...
        pdf = fitz.open("table.pdf")
        self.highlight_text(pdf, "12345678")
...

相关问题