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
。
2条答案
按热度按时间vh0rcniy1#
找到了答案,
p5cysglq2#
可以使用
Shape
对象绘制圆:以上是下面代码的结果: