文档抽取功能会根据文件分类字段配置,识别文档中的关键字段信息。每个字段都包含键值对和位置坐标信息。

字段结构

字段信息位于 result.files[].data.fields[] 中,每个字段包含以下属性:
  • key: 字段名称(如”发票代码”、“开票日期”等)
  • value: 字段值(识别出的文本内容)
  • position[]: 字段在文档中的位置坐标信息, 字段可能跨页或跨行,因此用数组表示

位置坐标结构

{
  "page": 0,           // 字段所在的页码
  "vertices": [        // 四个顶点的坐标 [x1,y1,x2,y2,x3,y3,x4,y4]
    100, 200,          // 左上角
    300, 200,          // 右上角
    300, 250,          // 右下角
    100, 250           // 左下角
  ]
}
详细的坐标说明请参考坐标系文档。

示例代码

import requests
import json

def extract_fields(workspace_id, batch_number, app_id, secret_code):
    """提取文档中的字段信息"""
    
    host = "https://docflow.textin.com"
    url = "/api/app-api/sip/platform/v2/file/fetch"
    
    resp = requests.get(
        f"{host}{url}",
        params={
            "workspace_id": workspace_id, 
            "batch_number": batch_number
        },
        headers={
            "x-ti-app-id": app_id, 
            "x-ti-secret-code": secret_code
        },
        timeout=60,
    )
    
    if resp.status_code != 200:
        print(f"请求失败: {resp.status_code}")
        return None
    
    data = resp.json()
    
    for file in data.get("result", {}).get("files", []):
        print(f"文件名: {file.get('name')}")
        print(f"识别状态: {file.get('recognition_status')}")
        
        # 提取字段信息
        fields = file.get("data", {}).get("fields", [])
        if fields:
            print("\n=== 字段信息 ===")
            for field in fields:
                key = field.get("key", "")
                value = field.get("value", "")
                positions = field.get("position", [])
                
                print(f"字段: {key}")
                print(f"值: {value}")
                
                # 显示位置信息
                for i, pos in enumerate(positions):
                    page = pos.get("page", 0)
                    vertices = pos.get("vertices", [])
                    print(f"  位置 {i+1} (第{page+1}页): {vertices}")
                print("-" * 30)
        else:
            print("未找到字段信息")
    
    return data

# 使用示例
if __name__ == "__main__":
    workspace_id = "<your-workspace-id>"
    batch_number = "<your-batch-number>"
    app_id = "<your-app-id>"
    secret_code = "<your-secret-code>"
    
    result = extract_fields(workspace_id, batch_number, app_id, secret_code)

返回数据示例

{
  "code": 200,
  "result": {
    "files": [
      {
        "id": "202412190001",
        "name": "invoice.pdf",
        "recognition_status": 1,
        "data": {
          "fields": [
            {
              "key": "发票代码",
              "value": "3100231130",
              "position": [
                {
                  "page": 0,
                  "vertices": [100, 150, 200, 150, 200, 180, 100, 180]
                }
              ]
            },
            {
              "key": "发票号码",
              "value": "12345678",
              "position": [
                {
                  "page": 0,
                  "vertices": [250, 150, 320, 150, 320, 180, 250, 180]
                }
              ]
            },
            {
              "key": "开票日期",
              "value": "2024年12月19日",
              "position": [
                {
                  "page": 0,
                  "vertices": [400, 150, 500, 150, 500, 180, 400, 180]
                }
              ]
            },
            {
              "key": "购买方名称",
              "value": "上海某某科技有限公司",
              "position": [
                {
                  "page": 0,
                  "vertices": [100, 250, 400, 250, 400, 280, 100, 280]
                }
              ]
            },
            {
              "key": "金额",
              "value": "1000.00",
              "position": [
                {
                  "page": 0,
                  "vertices": [500, 350, 600, 350, 600, 380, 500, 380]
                }
              ]
            }
          ]
        }
      }
    ]
  }
}