使用python突出显示pdf文件中的文本内容并保存屏幕截图

问题描述 投票:-1回答:1

我有一个pdf文件列表,我需要在这些文件的每个页面上突出显示特定文本,并为每个文本实例保存快照。

到目前为止,我能够突出显示文本并将pdf文件的整个页面保存为快照。但是,我想找到突出显示文本的位置并放大快照,与完整页面快照相比,它将更加详细。

我很确定必须有解决这个问题的方法。我是Python的新手,因此我无法找到它。如果有人可以帮我解决这个问题,我将非常感激。

我尝试过使用PyPDF2Pymupdf库,但我无法找到解决方案。我也尝试通过提供有效的坐标来突出显示但无法找到将这些坐标作为输出的方法。

[![Sample snapshot from the code[![\]\[1\]][1]][1]][1]

#import PyPDF2
import os
import fitz
from wand.image import Image
import csv
#import re
#from pdf2image import convert_from_path

check = r'C:\Users\Pradyumna.M\Desktop\Pradyumna\Automation\Intel Bytes\Create Source Docs\Sample Check 8 Apr 2019'

dir1 = check + '\\Source Docs\\'
dir2 = check + '\\Output\\'

dir = [dir1, dir2]

for x in dir:
    try:
        os.mkdir(x)
    except FileExistsError:
        print("Directory ", x, " already exists")

### READ PDF FILE
with open('upload1.csv', newline='') as myfile:
    reader = csv.reader(myfile)
    for row in reader:
        rowarray = '; '.join(row)
        src = rowarray.split("; ")
        file = check + '\\' + src[4] + '.pdf'
        print(file)
        #pdfFileObj = open(file,'rb')
        #pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
        #print("Total number of pages: " + str(pdfReader.numPages))
        doc = fitz.open(file)
        print(src[5])
        for i in range(int(src[5])-1, int(src[5])):
            i = int(i)
            page = doc[i]
            print("Processing page: " + str(i))
            text = src[3]
            #SEARCH TEXT
            print("Searching: " + text)
            text_instances = page.searchFor(text)
            for inst in text_instances:
                highlight = page.addHighlightAnnot(inst)
                file1 = check + '\\Output\\' + src[4] + '_output.pdf'
                print(file1)
                doc.save(file1, garbage=4, deflate=True, clean=True)
                ### Screenshot
                with(Image(filename=file1, resolution=150)) as source:
                    images = source.sequence
                    newfilename = check + "\\Source Docs\\" + src[0] + '.jpeg'
                    Image(images[i]).save(filename=newfilename)
                    print("Screenshot of " + src[0] + " saved")
python pdf pypdf2
1个回答
3
投票

“找不到将这些坐标作为输出的方法” - 你可以通过这样做得到坐标:

for inst in text_instances:
    print(inst)

instfitz.Rect对象,包含找到的文本片段的左上角和右下角坐标。所有信息都在docs中提供。

我设法突出显示点,并使用以下代码片段保存裁剪区域。我使用的是python 3.7.1,fitz.version的输出是('1.14.13', '1.14.0', '20190407064320')

import fitz

doc = fitz.open("foo.pdf")
inst_counter = 0
for pi in range(doc.pageCount):
    page = doc[pi]

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

    five_percent_height = (page.rect.br.y - page.rect.tl.y)*0.05

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

        # define a suitable cropping box which spans the whole page 
        # and adds padding around the highlighted text
        tl_pt = fitz.Point(page.rect.tl.x, max(page.rect.tl.y, inst.tl.y - five_percent_height))
        br_pt = fitz.Point(page.rect.br.x, min(page.rect.br.y, inst.br.y + five_percent_height))
        hl_clip = fitz.Rect(tl_pt, br_pt)

        zoom_mat = fitz.Matrix(2, 2)
        pix = page.getPixmap(matrix=zoom_mat, clip = hl_clip)
        pix.writePNG(f"pg{pi}-hl{inst_counter}.png")

doc.close()

我在一个样本pdf上测试了这个,我用“hello”:Input image

脚本的一些输出:pg2-hello1 pg2-hello5

我从文档的以下页面中编写了解决方案:

© www.soinside.com 2019 - 2024. All rights reserved.