繁体   English   中英

如何在pdf中提取输入字段的x0,y0坐标

[英]how to extract x0, y0 coordinates of input field in pdf

我想抓取一个 pdf 文档,我想要输入字段的坐标(文本字段的左下角点)。 有没有办法使用像pyPDF2或pdfMiner这样的python库来实现这一点? 以下图片可能有助于理解问题

看图片

通常,这些字段要么是句号的重复,要么是下划线。 您可以使用 PyMuPDF 提取 pdf 文件的文本行并使用正则表达式 ( import re ) 来识别此类重复,然后在识别出匹配项时将坐标保存到列表或类似内容中。

下面的代码执行此操作,只是将 (x0,y0,x1,y1) 保存为左下角 (x0,y0) 和右上角 (x1,y1) 的坐标 - 您可以提取所需的坐标。

    def whichFields(self, txtline):
        reg = re.compile(r"(…|\..)\1+")
        self.matches.append(reg.finditer(txtline))
        return self.matches

    # Uses PyMuPDF to find box coordinates of the fields in matches[]
    # returns a list of the coordinates in the order which they
    # appear in matches[].
    def whereFields(self):
        global c
        count = 0
        for page in self.doc:
            field_areas = []
            c = self.newCanvas(count)
            page_num = count
            count += 1
            mts = []
            txtlines = page.getText("text").split("\n")  # using doc opened in fitz, splitting all text lines in page
            prev_area = []
            for j in txtlines:
                mts.append(self.whichFields(j))

            # These for loops access the result of the regex search and then ultimately pass
            # the matching strings to searchFor() which returns a list of coordinates of the
            # rectangles in which the searched "fields" are found.
            for data in mts:
                for match in data:
                    for i in match:
                        # extracts the matching string and searches for its rect coordinates.
                        self.areas = page.searchFor(i[1])
                        for area in self.areas:
                            field_areas.append(area)
`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM