通过 Python 从 PDF 中提取表格

从 PDF 文档中提取表格。使用 Aspose.PDF for Python for .NET 以编程方式修改 PDF 文件

如何使用 Python for .NET 庫從PDF文檔中提取表格

為了提取表格,我們將使用 [Aspose.PDF 用於 .NET](https://products.aspose.com/pdf/net) API,這是一個功能豐富、功能強大且易於使用的文檔操作 API,適用於 python-net 平臺。打開 [NuGet](https://www.nuget.org/packages/aspose.pdf) 包管理器,搜索“.PDF”並安裝。您也可以從程式包管理器主控台使用以下命令。

Python Package Manager Console

pip install aspose-pdf

通過Python從PDF中提取表格


您需要 [Aspose.PDF 用於 .NET](https://releases.aspose.com/pdf/net) 在您的環境中嘗試代碼。

  1. 載入包含文件實例的 PDF。
  2. 建立表吸收器物件以查找表。
  3. 使用吸收器訪問第一頁。
  4. 取得頁面上的第一個表。
  5. 刪除表。保存檔案。

從 PDF 中提取表格 - Python

import aspose.pdf as ap

input_file = DIR_INPUT_TABLE + "Table_input.pdf"
# Load source PDF document
pdf_document = ap.Document(input_file)
for page in pdf_document.pages:
    absorber = ap.text.TableAbsorber()
    absorber.visit(page)
    for table in absorber.table_list:
        for row in table.row_list:
            for cell in row.cell_list:
                text_fragment_collection = cell.text_fragments
                for fragment in text_fragment_collection:
                    txt = ""
                    for seg in fragment.segments:
                        txt += seg.text
                    print(txt)