为了更准确地提取表单元格中嵌入的类似表格的数据,我希望能够识别PDF中的表单元格边界,如下所示:
我已经尝试使用Camelot、pdfplumber和PyMuPDF来提取这样的表,并取得了不同程度的成功。但由于我们收到的PDF格式不一致,即使在指定表格边界时,我也无法可靠地获得准确的结果。
我发现,如果我通过显式指定单元格边界来单独提取每个表格单元格,结果会更好。我已经通过手动输入边界进行了测试,这是我使用Camelot的可视化调试工具获得的。
我的挑战是如何以编程方式识别表格单元格边界,因为表格可以从页面上的任何位置开始,并且单元格的垂直高度是可变的。
在我看来,人们可以通过找到行分隔线的坐标来做到这一点,这对人类来说是如此明显。但是我还不知道如何使用python工具找到这些行。这是可能的吗,或者有其他/更好的方法来解决这个问题吗?
发布于 2021-06-02 14:52:22
我最近有一个类似的用例,我需要通过代码本身弄清楚边界。对于您的用例,有两个选项:
import pdfplumber
pdf = pdfplumber.open('file_name.pdf')
p0 = pdf.pages[req_page] # go to the required page
tables = p0.debug_tablefinder() # list of tables which pdfplumber identifies
req_table = tables.tables[i] # Suppose you want to use ith table
req_table.bbox # gives you the bounding box of the table (coordinates)
import pdfplumber
pdf = pdfplumber.open('file_name.pdf')
p0 = pdf.pages[req_page] # go to the required page
tables = p0.debug_tablefinder() # list of tables which pdfplumber identifies
req_table = tables.tables[i] # Suppose you want to use ith table
cells = req_table.cells # gives list of all cells in that table
for cell in cells[i:j]: # iterating through the required cells
p0.crop(cell).extract_words() # extract the words
https://stackoverflow.com/questions/66463612
复制相似问题