功能概述

对于包含多份、多类别文件的复杂文档,文件拆分功能支持智能识别文档内容,实现文档的自动拆分及分类。

使用场景

1. 医疗保险理赔场景

一份多页文件包含:
  • 第1-2页:保单信息
  • 第3-5页:医疗发票
  • 第6-10页:住院记录
通过文件拆分功能,可以将这三种不同类型的文档分别拆分出来,便于后续的分类和抽取处理。

2. 物流进出口场景

一份文件包含:
  • 第1页:出口报关单
  • 第2页:商业发票
  • 第3页:装箱单(Packing List)
  • 第4页:销售合同
文件拆分功能可以按文档类型进行智能拆分。

API 参数配置

启用文件拆分功能

在上传接口中设置 split_flag=true 来启用文件拆分功能:
curl -X POST \
  -H "x-ti-app-id: <your-app-id>" \
  -H "x-ti-secret-code: <your-secret-code>" \
  -F "file=@/path/to/multi-page-document.pdf" \
  "https://docflow.textin.com/api/app-api/sip/platform/v2/file/upload?workspace_id=<your-workspace-id>&split_flag=true"

参数说明

参数名类型默认值说明
split_flagbooleanfalse是否启用文件拆分功能

示例代码

import requests
import json

def upload_with_split(file_path, workspace_id, app_id, secret_code):
    """
    上传文件并启用文件拆分功能
    """
    url = "https://docflow.textin.com/api/app-api/sip/platform/v2/file/upload"
    
    headers = {
        "x-ti-app-id": app_id,
        "x-ti-secret-code": secret_code
    }
    
    params = {
        "workspace_id": workspace_id,
        "split_flag": "true"  # 启用文件拆分功能
    }
    
    with open(file_path, 'rb') as file:
        files = {'file': file}
        response = requests.post(url, headers=headers, params=params, files=files)
    
    return response.json()

def fetch_split_results(workspace_id, batch_number, app_id, secret_code):
    """
    查询文件拆分结果
    """
    url = "https://docflow.textin.com/api/app-api/sip/platform/v2/file/fetch"
    
    headers = {
        "x-ti-app-id": app_id,
        "x-ti-secret-code": secret_code
    }
    
    params = {
        "workspace_id": workspace_id,
        "batch_number": batch_number
    }
    
    response = requests.get(url, headers=headers, params=params)
    return response.json()

# 使用示例
if __name__ == "__main__":
    # 配置信息
    WORKSPACE_ID = "your-workspace-id"
    APP_ID = "your-app-id"
    SECRET_CODE = "your-secret-code"
    FILE_PATH = "/path/to/multi-page-document.pdf"
    
    # 上传文件并启用文件拆分
    upload_result = upload_with_split(FILE_PATH, WORKSPACE_ID, APP_ID, SECRET_CODE)
    print("上传结果:", json.dumps(upload_result, indent=2, ensure_ascii=False))
    
    # 获取批次号
    batch_number = upload_result.get("result", {}).get("batch_number")
    
    if batch_number:
        # 查询文件拆分结果
        fetch_result = fetch_split_results(WORKSPACE_ID, batch_number, APP_ID, SECRET_CODE)
        print("文件拆分结果:", json.dumps(fetch_result, indent=2, ensure_ascii=False))

返回结果说明

文件拆分结果结构

当启用文件拆分功能后,file/fetch 接口返回的结果中会包含 child_files 字段,用于描述拆分后的子文档信息:
{
  "code": 200,
  "result": {
    "files": [
      {
        "id": "parent-file-001",
        "name": "multi-document.pdf",
        "format": "pdf",
        "child_files": [
          {
            "id": "child-001",
            "task_id": "task-001",
            "task_type": 0,  // 0表示文件拆分产生的子文件
            "name": "multi-document.pdf#1",
            "format": "pdf",
            "category": "invoice",
          },
          {
            "id": "child-002", 
            "task_id": "task-002",
            "task_type": 0,
            "name": "multi-document.pdf#2",
            "format": "pdf",
            "category": "contract",
          }
        ]
      }
    ]
  }
}

关键字段说明

字段名类型说明
child_filesarray拆分后的子文件列表
child_files[].idstring子文件唯一标识
child_files[].task_typeinteger任务类型,0表示文件拆分产生
child_files[].categorystring文档分类结果
child_files[].pagesstring拆分后的子文件页面信息,含子文件在原文件中的页码