在PDF文件中查找文本位置 [英] Find text position in PDF file

查看:796
本文介绍了在PDF文件中查找文本位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PDF文件,我试图在PDF中找到特定的文本并使用Python突出显示. 我找到了 PyPDF2 ,它可以

I have a PDF file and I am trying to find a specific text in the PDF and highlight it using Python. I found PyPDF2, which can highlight part of a PDF when we give the coordinates of the wanted highlight position in the file.

我正在尝试找到一种工具,该工具可以使给定文本在PDF中的位置.

I am trying to find a tool which can give me the position of a given text in the PDF.

推荐答案

PyMuPDF 可以按坐标查找文本.您可以将其与 PyPDF2高亮方法结合使用以完成您所描述的内容.或者,您可以使用PyMuPDF突出显示文本.

PyMuPDF can find text by coordinates. You can use this in combination with the PyPDF2 highlighting method to accomplish what you're describing. Or you can just use PyMuPDF to highlight the text.

以下是使用PyMuPDF查找文本和突出显示的示例代码:

Here is sample code for finding text and highlighting with PyMuPDF:

import fitz

### READ IN PDF

doc = fitz.open("input.pdf")
page = doc[0]

text = "Sample text"
text_instances = page.searchFor(text)

### HIGHLIGHT

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


### OUTPUT

doc.save("output.pdf", garbage=4, deflate=True, clean=True)

这篇关于在PDF文件中查找文本位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆