HTML JPG PDF XML DOCX
  Product Family
PDF

在 Python SDK 中解析 PDF 以提取表格

使用服务器端 Python API 解析 PDF 文档以提取表格的 API。

Get Started

如何通过云端 Python SDK 解析 PDF 文档以提取表格

为了通过 Cloud Python SDK 解析 PDF 文档以提取表格,我们将使用 Aspose.PDF Cloud Python SDK 此 Cloud SDK 帮助 Python 程序员使用 Aspose.PDF REST API 开发基于云的 PDF 创建、注释、编辑、转换和解析应用程序。只需在 Aspose for Cloud 创建一个帐户并获取您的应用程序信息。一旦您拥有了 App SID 和密钥,就可以开始使用 Aspose.PDF Cloud Python SDK。如果 Python 包托管在 Github 上,您可以直接从 Github 安装:

从 Github 安装


     
    pip install git+https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-python.git
     
     

使用 Python SDK 解析 PDF 以提取表格的步骤

Aspose.PDF Cloud 开发人员可以轻松解析 PDF 文档以提取表格。开发人员只需几行代码。

  1. 使用您的应用程序密钥和密钥创建一个新的配置对象
  2. 创建一个对象来连接到云 API
  3. 上传您的文档文件
  4. 使用 get_document_tables 函数在云存储中解析 PDF 文档以提取表格
  5. 检查响应并记录结果
  6. 如有需要,在本地以 JSON 文件格式下载表格信息
 

此示例代码展示了解析 PDF 文档以提取表格


import shutil
import json
import logging
from pathlib import Path
from asposepdfcloud import ApiClient, PdfApi
import logging

# Configure logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")


class ParseExtractTables:
    """Class for extracting tables from PDF document page using Aspose PDF Cloud API."""
    def __init__(self):
        self.pdf_api = PdfApi(ApiClient(APP_KEY, APP_SID)

    def upload_document(self, documentName: str, localFolder: str, remoteFolder: str):
        """Upload a PDF document to the Aspose Cloud server."""
        if self.pdf_api:
            file_path = localFolder / documentName
            try:
                if remoteFolder == None:
                    self.pdf_api.upload_file(documentName, str(file_path))
                else:
                    opts = { "folder": remoteFolder }
                    self.pdf_api.upload_file(remoteFolder + '/' + documentName, file_path)
                logging.info(f"File {documentName} uploaded successfully.")
            except Exception as e:
                logging.error(f"Failed to upload file: {e}")

   def extract(self, documentName: str, localFolder: Path, remoteFolder: Path):
        self.upload_document(documentName, remoteFolder)

        opts = {
            "folder": remoteFolder
        }
        respTables = self.pdf_api.get_document_tables(documentName, **opts)
        if respTables.code != 200:
            logging.error("GetTables(): Unexpected error!")
        else:
            localJsonFile = str(Path.joinpath(localFolder, "tables_objects.json"))
            with open(localJsonFile, "w", encoding="utf-8") as localFile:
                localFile.write("[\n")

                for tab in respTables.tables.list:
                    logging.info(f"GetTabels(): Table '{tab.id}' successfully extracted from the document '{documentName}'.")
                    jsText = json.dumps(tab.__dict__, ensure_ascii=False, default=str, indent=4) + ",\n\n"
                    localFile.write(jsText)
                
                localFile.write("]")
 

通过 Python SDK 处理 PDF 中的表格解析

通过解析 PDF 文档以提取表格,您可以根据需要修改表格的内容。这可保持表格在文档中的位置,同时节省时间并减少手动工作。 使用 Aspose.PDF Cloud Python SDK 解析 PDF 文档以提取表格。

使用我们的 Python SDK,您可以

  • 以文本或图像格式添加 PDF 文档的页眉和页脚。
  • 向 PDF 文档添加表格和文本或图像印章。
  • 将多个 PDF 文档追加到现有文件。
  • 处理 PDF 附件、注释和表单字段。
  • 对 PDF 文档应用加密或解密并设置密码。
  • 从页面或整个 PDF 文档中删除所有印章和表格。
  • 根据 ID 从 PDF 文档中删除特定的印章或表格。
  • 替换 PDF 页面上或整个文档中的单个或多个文本实例。
  • 广泛支持将 PDF 文档转换为各种其他文件格式。
  • 提取 PDF 文件的各种元素并优化 PDF 文档。
  • 您可以尝试我们的 免费应用 来测试功能。

  •