【问题标题】:Extracting text from highlighted annotations in a PDF file从 PDF 文件中突出显示的注释中提取文本
【发布时间】:2014-01-10 17:24:16
【问题描述】:

从昨天开始,我一直在尝试使用 python-poppler-qt4 从一个 pdf 中的一些突出显示的注释中提取文本。

根据this documentation,看起来我必须使用 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()

测试pdf: https://www.dropbox.com/s/10plnj67k9xd1ot/test.pdf

【问题讨论】:

    标签: python qt pdf poppler


    【解决方案1】:

    查看the documentation for Annotations 似乎边界属性以标准化坐标返回此注解的边界矩形。 虽然这似乎是一个奇怪的决定,但我们可以简单地通过page.pageSize().width() 和@987654326 缩放坐标@值。

    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()):
            #print("========= PAGE {} =========".format(i+1))
            page = doc.page(i)
            annotations = page.annotations()
            (pwidth, pheight) = (page.pageSize().width(), page.pageSize().height())
            if len(annotations) > 0:
                for annotation in annotations:
                    if  isinstance(annotation, popplerqt4.Poppler.Annotation):
                        total_annotations += 1
                        if(isinstance(annotation, popplerqt4.Poppler.HighlightAnnotation)):
                            quads = annotation.highlightQuads()
                            txt = ""
                            for quad in quads:
                                rect = (quad.points[0].x() * pwidth,
                                        quad.points[0].y() * pheight,
                                        quad.points[2].x() * pwidth,
                                        quad.points[2].y() * pheight)
                                bdy = PyQt4.QtCore.QRectF()
                                bdy.setCoords(*rect)
                                txt = txt + unicode(page.text(bdy)) + ' '
    
                            #print("========= ANNOTATION =========")
                            print(unicode(txt))
    
        if total_annotations > 0:
            print str(total_annotations) + " annotation(s) found"
        else:
            print "no annotations found"
    
    if __name__ == "__main__":
        main()
    

    此外,我决定连接 .highlightQuads() 以更好地表示实际突出显示的内容。

    请注意我已附加到每个四边形文本区域的显式 <space>

    在示例文档中,返回的QString 不能直接传递给print()str(),解决方法是改用unicode()

    我希望这对某人有所帮助。

    注意:页面旋转可能会影响缩放值,我无法对此进行测试。

    【讨论】:

    猜你喜欢
    • 2014-06-14
    • 1970-01-01
    • 2016-05-01
    • 2012-02-24
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    • 1970-01-01
    相关资源
    最近更新 更多