从昨天开始,我尝试使用 python-poppler-qt4 从一个 pdf 中的一些突出显示的注释中提取文本。
根据这个文档,看起来我必须使用 Page.text() 方法获取文本,从突出显示的注释中传递一个 Rectangle 参数,我使用 Annotation.boundary() 得到。但我只得到空白文本。有人能帮我吗?我在下面复制了我的代码,并为我正在使用的 PDF 添加了一个链接。谢谢你的帮助!
import popplerqt4
import sys
import PyQt4
def main():
doc = popplerqt4.Poppler.Document.load(sys.argv[1])
total_annotations = 0
for i in range(doc.numPages()):
page = doc.page(i)
annotations = page.annotations()
if len(annotations) > 0:
for annotation in annotations:
if isinstance(annotation, popplerqt4.Poppler.Annotation):
total_annotations += 1
if(isinstance(annotation, popplerqt4.Poppler.HighlightAnnotation)):
print str(page.text(annotation.boundary()))
if total_annotations > 0:
print str(total_annotations) + " annotation(s) found"
else:
print "no annotations found"
if __name__ == "__main__":
main()