【问题标题】:Camelot - detecting hyperlinks within tableCamelot - 检测表内的超链接
【发布时间】:2022-12-03 01:06:41
【问题描述】:

我正在使用 Camelot 从 PDF 文件中提取表格。虽然这非常有效,但它只提取文本,不会提取嵌入在表格中的超链接。

有没有办法使用 Camelot 或类似的包来提取表格中嵌入的表格文本和超链接?

谢谢!

【问题讨论】:

  • 你能发布一个PDF示例吗?
  • 请参阅第 2 页的 here
  • 站在这个问题上(github.com/camelot-dev/camelot/issues/137),目前,这个功能没有在Camelot中实现。甚至 tabula 库也没有实现此功能。

标签: python pdf python-camelot


【解决方案1】:

大多数应用程序(例如表格文本提取器)只是将可见表面刮为纯文本,实际上超链接通常存储在 pdf 的其他位置,这不是 WTSIWYG 文字处理器文件。

所以,如果你幸运的话,你可以提取坐标(没有像这样的页面分配)

C:Userslz02Downloads>type "7 - 20 November 2022 (003).pdf" |findstr /i "(http"
<</Subtype/Link/Rect[ 69.75 299.75 280.63 313.18] /BS<</W 0>>/F 4/A<</Type/Action/S/URI/URI(http://www.bbc.co.uk/complaints/complaint/) >>/StructParent 5>>
<</Subtype/Link/Rect[ 219.37 120.85 402.47 133.06] /BS<</W 0>>/F 4/A<</Type/Action/S/URI/URI(http://www.bbc.co.uk/complaints/handle-complaint/) >>/StructParent 1>>
<</Subtype/Link/Rect[ 146.23 108.64 329.33 120.85] /BS<</W 0>>/F 4/A<</Type/Action/S/URI/URI(http://www.bbc.co.uk/complaints/handle-complaint/) >>/StructParent 2>>
<</Subtype/Link/Rect[ 412.48 108.64 525.55 120.85] /BS<</W 0>>/F 4/A<</Type/Action/S/URI/URI(https://www.ofcom.org.uk/tv-radio-and-on-demand/broadcast-codes/broadcast-code) >>/StructParent 3>>
<</Subtype/Link/Rect[ 69.75 96.434 95.085 108.64] /BS<</W 0>>/F 4/A<</Type/Action/S/URI/URI(https://www.ofcom.org.uk/tv-radio-and-on-demand/broadcast-codes/broadcast-code) >>/StructParent 4>>
<</Subtype/Link/Rect[ 69.75 683.75 317.08 697.18] /BS<</W 0>>/F 4/A<</Type/Action/S/URI/URI(http://www.bbc.co.uk/complaints/comp-reports/ecu/) >>/StructParent 7>>
<</Subtype/Link/Rect[ 463.35 604.46 500.24 617.89] /BS<</W 0>>/F 4/A<</Type/Action/S/URI/URI(https://www.bbc.co.uk/contact/ecu/reporting-scotland-bbc-one-scotland-20-december-2021) >>/StructParent 8>>
<</Subtype/Link/Rect[ 463.35 577.11 500.24 590.54] /BS<</W 0>>/F 4/A<</Type/Action/S/URI/URI(https://www.bbc.co.uk/contact/ecu/book-of-the-week-preventable-radio-4-19-april-2022) >>/StructParent 9>>
<</Subtype/Link/Rect[ 463.35 522.4 521.41 535.83] /BS<</W 0>>/F 4/A<</Type/Action/S/URI/URI(https://www.bbc.co.uk/contact/ecu/the-one-show-bbc-one-6-october-2022) >>/StructParent 10>>
<</Subtype/Link/Rect[ 463.35 495.04 518.04 508.47] /BS<</W 0>>/F 4/A<</Type/Action/S/URI/URI(https://www.bbc.co.uk/contact/ecu/news-6pm-bbc-one-22-september-2022) >>/StructParent 11>>
<</Subtype/Link/Rect[ 463.35 469.04 518.04 482.47] /BS<</W 0>>/F 4/A<</Type/Action/S/URI/URI(https://www.bbc.co.uk/contact/ecu/news-1030am-bbc-news-channel-20-september-2022) >>/StructParent 12>>

注意,随机顺序, 要找到它们属于哪个页面,您需要回溯到它们的 /StructParent ##

【讨论】:

    【解决方案2】:

    是的,这是可能的。默认情况下,Camelot 仅从 PDF 文件中提取文本,但它也提供了提取附加信息的选项,例如文本块的位置和大小,以及定义表格单元格的直线和曲线的坐标。使用此信息,可以识别包含超链接的表格单元格,并提取每个单元格的文本和超链接目标。

    以下是如何使用 Camelot 完成此操作的示例:

    import camelot
    
    # Load the PDF file
    pdf = camelot.read_pdf("example.pdf")
    
    # Extract the tables, including their coordinates and text blocks
    tables = pdf.extract(flavor="lattice", tables=None, spreadsheets=None,
                         str_columns_map=None, columns=None, suppress_stdout=False)
    
    # Iterate over the tables
    for table in tables:
      # Iterate over the rows in the table
      for row in table.data:
        # Iterate over the cells in the row
        for cell in row:
          # If the cell contains a hyperlink, extract the text and the hyperlink destination
          if cell.text.startswith("http"):
            text = cell.text
            hyperlink = cell.bbox[0]
            print(text, hyperlink)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 2014-06-09
      • 1970-01-01
      • 2012-07-20
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      相关资源
      最近更新 更多