表格是文件类别中用于抽取结构化表格数据的配置。每个表格可以包含多个字段,用于定义表格的列。本文介绍如何通过 API 管理文件类别下的表格。
获取表格列表
获取指定文件类别下的所有表格列表:
curl \
-H "x-ti-app-id: <your-app-id>" \
-H "x-ti-secret-code: <your-secret-code>" \
"https://docflow.textin.com/api/app-api/sip/platform/v2/category/tables/list?workspace_id=<your-workspace-id>&category_id=<category-id>"
请求参数:
workspace_id (必填): 工作空间 ID
category_id (必填): 文件类别 ID
响应示例:
{
"code": 200,
"msg": "success",
"result": {
"tables": [
{
"id": "table_123",
"name": "货物明细",
"prompt": "请抽取每行的品名、数量和金额",
"collect_from_multi_table": true
},
{
"id": "table_456",
"name": "费用明细",
"prompt": "请抽取每行的费用项目和金额",
"collect_from_multi_table": false
}
]
}
}
新增表格
在指定文件类别下新增一个表格:
curl -X POST \
-H "x-ti-app-id: <your-app-id>" \
-H "x-ti-secret-code: <your-secret-code>" \
-H "Content-Type: application/json" \
-d '{
"workspace_id": "<your-workspace-id>",
"category_id": "<category-id>",
"name": "货物明细",
"prompt": "请抽取每行的品名、数量和金额",
"collect_from_multi_table": true
}' \
"https://docflow.textin.com/api/app-api/sip/platform/v2/category/tables/add"
请求参数:
workspace_id (必填): 工作空间 ID
category_id (必填): 文件类别 ID
name (必填): 表格名称,最大长度 50
prompt (选填): 表格语义抽取提示词,最大长度 200
collect_from_multi_table (选填): 是否多表合并,默认为 false
响应示例:
{
"code": 200,
"msg": "success",
"result": {
"table_id": "table_new_789"
}
}
多表合并说明:当文档中存在多个结构相同的表格时,开启 collect_from_multi_table 可以将它们合并为一个表格结果。例如,发票明细表格可能跨页显示,开启多表合并后会将所有页的明细合并到一起。
更新表格
更新指定表格的信息:
curl -X POST \
-H "x-ti-app-id: <your-app-id>" \
-H "x-ti-secret-code: <your-secret-code>" \
-H "Content-Type: application/json" \
-d '{
"workspace_id": "<your-workspace-id>",
"category_id": "<category-id>",
"table_id": "<table-id>",
"name": "更新后的表格名称",
"prompt": "更新后的提示词",
"collect_from_multi_table": true
}' \
"https://docflow.textin.com/api/app-api/sip/platform/v2/category/tables/update"
请求参数:
workspace_id (必填): 工作空间 ID
category_id (必填): 文件类别 ID
table_id (必填): 表格 ID
name (选填): 表格名称,最大长度 50
prompt (选填): 表格语义抽取提示词,最大长度 200
collect_from_multi_table (必填): 是否多表合并
删除表格
删除指定的表格,支持批量删除:
curl -X POST \
-H "x-ti-app-id: <your-app-id>" \
-H "x-ti-secret-code: <your-secret-code>" \
-H "Content-Type: application/json" \
-d '{
"workspace_id": "<your-workspace-id>",
"category_id": "<category-id>",
"table_ids": ["table_123", "table_456"]
}' \
"https://docflow.textin.com/api/app-api/sip/platform/v2/category/tables/delete"
请求参数:
workspace_id (必填): 工作空间 ID
category_id (必填): 文件类别 ID
table_ids (必填): 要删除的表格 ID 数组
删除表格会同时删除该表格下的所有字段,请谨慎操作。
表格字段管理
创建表格后,需要为表格添加字段来定义表格的列。表格字段的管理请参考字段管理文档中的”新增表格字段”部分。
示例:创建表格并添加字段
import requests
ti_app_id = "<your-app-id>"
ti_secret_code = "<your-secret-code>"
workspace_id = "<your-workspace-id>"
category_id = "<category-id>"
host = "https://docflow.textin.com"
# 1. 创建表格
table_resp = requests.post(
url=f"{host}/api/app-api/sip/platform/v2/category/tables/add",
json={
"workspace_id": workspace_id,
"category_id": category_id,
"name": "货物明细",
"prompt": "请抽取每行的品名、数量和金额",
"collect_from_multi_table": True
},
headers={
"x-ti-app-id": ti_app_id,
"x-ti-secret-code": ti_secret_code,
},
)
table_id = table_resp.json()["result"]["table_id"]
print(f"表格创建成功,ID: {table_id}")
# 2. 为表格添加字段
fields_to_add = [
{"name": "货物名称", "description": "货物或服务名称"},
{"name": "数量", "description": "货物数量"},
{"name": "单价", "description": "货物单价"},
{"name": "金额", "description": "货物金额"}
]
for field_config in fields_to_add:
field_resp = requests.post(
url=f"{host}/api/app-api/sip/platform/v2/category/fields/add",
json={
"workspace_id": workspace_id,
"category_id": category_id,
"table_id": table_id, # 指定表格ID
**field_config
},
headers={
"x-ti-app-id": ti_app_id,
"x-ti-secret-code": ti_secret_code,
},
)
field_id = field_resp.json()["result"]["field_id"]
print(f"字段 '{field_config['name']}' 创建成功,ID: {field_id}")
print("表格及字段创建完成")
表格配置说明
表格属性
- name: 表格名称,必填
- prompt: 表格语义抽取提示词,用于指导 AI 进行表格抽取
- collect_from_multi_table: 是否多表合并
多表合并场景
以下场景适合开启多表合并:
- 跨页表格:表格内容跨越多页显示
- 重复表格:文档中存在多个结构相同的表格
- 分段表格:表格被其他内容分隔成多段
示例:发票货物明细表格如果跨越多页,开启多表合并后,所有页的货物明细会合并成一个完整的表格结果。
下一步