【问题标题】:How to select the value of a searched text in PDF using Python?如何使用 Python 选择 PDF 中搜索文本的值?
【发布时间】:2020-05-19 11:11:50
【问题描述】:

PDF 包含以下信息:

      Name: ABC
Profession: XYZ
   Hobbies: HIJ 

使用 PyMUPDF 我可以在 PDF 中搜索一个单词并突出显示它,但我想突出显示搜索文本的右侧值。
在此示例中,我想突出显示名称的值,即 ABC。因为对于不同的文件,名称值可能会从 ABC 更改为 PQR。

任何人都可以帮助如何做到这一点?非常感谢!

【问题讨论】:

    标签: python pdf text pymupdf


    【解决方案1】:

    我想我自己找到了解决办法,

    import fitz                           #import dependencies
    doc  = fitz.open('filename.pdf')      #openPDF 
    page = doc[0]
    
    text = "Name"                         #Text intended to search
    rl   = page.searchFor(text)           #coordinates of the searched text saved as list (rl-rectangle)
    
    rect = rl[0]+(20,0,20,0)              #to move the rectangle 20 pixels to the right
    highlight = page.addHighlightAnnot(rect) # highlight
    
    doc.save("output.pdf", garbage=4, deflate=True, clean=True)
    

    根据移动矩形的方向和距离,经过几次试验后,必须近似或设置像素数。 希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2013-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-21
      • 2015-12-31
      • 1970-01-01
      • 2011-12-02
      • 2019-12-21
      相关资源
      最近更新 更多