文档解析(旧接口)
更新时间:2024-07-26
接口描述
支持对doc、pdf、图片、xlsx等16种格式文档进行解析,输出文档的版面、表格、阅读顺序、标题层级、旋转角度等信息,将非结构化数据转化为易于处理的结构化数据,识别准确率可达 90% 以上。
该接口正在公测中,完成个人/企业认证的用户可领取200页免费额度,如需申请更多额度或者QPS,请合作咨询,或者提交工单。
文档解析为异步接口,需要先调用提交请求接口获取 task_id,然后调用获取结果接口进行结果轮询,建议提交请求后 5~10 秒轮询。提交请求接口QPS为2,获取结果接口QPS为10。
提交请求接口
请求说明
请求示例
HTTP 方法:POST
请求URL: https://aip.baidubce.com/file/2.0/brain/online/v1/parser/task
URL参数:
参数 | 值 |
---|---|
access_token | 通过API Key和Secret Key获取的access_token,参考“Access Token获取” |
Header如下:
参数 | 值 |
---|---|
Content-Type | multipart/form-data |
Body中放置请求参数,参数详情如下:
请求参数
参数 | 是否必选 | 类型 | 可选值范围 | 说明 |
---|---|---|---|---|
file | 和file_url二选一 | file | - | 文件数据,支持的文件类型:-版式文档:pdf、jpg、jpeg、png、bmp、tif、tiff、ofd、ppt、pptx-流式文档:doc、docx、txt、xls、xlsx、wps文档大小不超过50M,文档页数不超过1000页(流式文档按2000字算一页)优先级: file > file_url,当file字段存在时,file_url字段失效 |
file_url | 和file二选一 | string | - | 文件完整URL,仅支持北京区域的BOS公网访问,URL长度不超过1024字节,支持格式与file一致,仅支持上传1篇文件,文件大小不超过50M优先级: file > file_url,当file字段存在时,file_url字段失效请注意关闭URL防盗链 |
file_name | 是 | string | - | 文件名,请保证文件名后缀正确,例如 "1.pdf " |
return_para_nodes | 否 | bool | true/false | 是否返回标题层级段落树。默认为false |
请求代码示例
提示:使用示例代码前,请记得替换其中的示例Token、文档地址或Base64信息。
Python
1import requests
2import os
3
4
5def create_task(url, file_path, file_url):
6 """
7 Args:
8 url: string, 服务请求链接
9 file_path: 本地文件路径
10 file_url: 文件链接
11 Returns: 响应
12 """
13 # 文件请求
14 body = {
15 "file": (os.path.basename(file_path), open(file_path, 'rb'), "multipart/form-data"),
16 }
17
18 # 文件链接请求
19 # body = {
20 # "file_url": (file_url, "multipart/form-data")
21 # }
22
23 data = {
24 "file_name": os.path.basename(file_path),
25 "return_para_nodes": True
26 }
27
28 response = requests.post(url, data=data, files=body)
29 return response
30
31if __name__ == '__main__':
32 request_host = "https://aip.baidubce.com/file/2.0/brain/online/v1/parser/task?" \
33 "access_token={token}"
34 file_path = "test.pdf"
35 response = create_task(request_host, file_path, "")
36 print(response.json())
返回说明
返回参数
字段 | 类型 | 说明 |
---|---|---|
log_id | uint64 | 唯一的log id,用于问题定位 |
error_code | int | 错误码 |
error_msg | string | 错误描述信息 |
result | dict | 返回的结果列表 |
+ task_id | string | 该请求生成的task_id,后续使用该task_id获取审查结果 |
返回示例
成功返回示例:
JSON
1{
2 "error_code": 0,
3 "error_msg": "",
4 "log_id": "10138598131137362685273505665433",
5 "result": {
6 "task_id": "task-3zy9Bg8CHt1M4pPOcX2q5bg28j26801S"
7 }
8}
失败返回示例(详细的错误码说明见API文档-错误码):
JSON
1{
2 "error_code": 282003,
3 "error_msg": "missing parameters",
4 "log_id": "37507631033585544507983253924141",
5 "result": "null"
6}
获取结果接口
请求说明
请求示例
HTTP 方法:POST
请求URL: https://aip.baidubce.com/file/2.0/brain/online/v1/parser/task/query
URL参数:
参数 | 值 |
---|---|
access_token | 通过API Key和Secret Key获取的access_token,参考“Access Token获取” |
Header如下:
参数 | 值 |
---|---|
Content-Type | multipart/form-data |
Body中放置请求参数,参数详情如下:
请求参数
参数 | 是否必选 | 类型 | 说明 |
---|---|---|---|
task_id | 是 | string | 发送提交请求时返回的task_id |
请求代码示例
提示:使用示例代码前,请记得替换其中的示例Token、task_id。
Python
1import requests
2
3def query_task(url, task_id):
4 """
5 Args:
6 url: string, 请求链接
7 task_id: string, task id
8 Returns: 响应
9 """
10 data = {
11 "task_id": task_id
12 }
13
14 response = requests.post(url, data=data, files=data)
15 return response
16
17
18if __name__ == '__main__':
19 task_id = "task-3ej6eh9m98OzWEovWlJv0B2pJpVhd1T0"
20 request_host = "https://aip.baidubce.com/file/2.0/brain/online/v1/parser/task/query?" \
21 "access_token={token}"
22 resp = query_task(request_host, task_id)
23 print(resp.json())
返回说明
返回参数
字段 | 类型 | 说明 |
---|---|---|
log_id | uint64 | 唯一的log id,用于问题定位 |
error_code | int | 错误码 |
error_msg | string | 错误描述信息 |
result | dict | 返回的结果列表 |
+ task_id | string | 任务ID |
+ status | string | 任务状态,pending:排队中;running:运行中;success:成功;failed:失败 |
+ task_error | string | 解析报错信息,包含任务失败、额度不够 |
+ duration | string | 任务执行时长 |
+ parse_result_url | string | 文档解析结果的bos链接 |
可通过parse_result_url下载解析结果的JSON文件,parse_result_url的返回参数如下:
字段 | 类型 | 说明 |
---|---|---|
file_name | string | 文档名称 |
file_content | list |
文档解析的内容 |
+ page_num | int | 页码,从0开始 |
+ page_size | dict | 页面大小,版式格式时有效 |
++ width | float | 页面宽度,版式格式时有效 |
++ height | float | 页面高度,版式格式时有效 |
+ page_angle | int | 页面旋转角度,版式格式时有效 |
+ is_scan | bool | 是否为扫描件 |
+ page_content | dict | 文档单页的解析内容 |
++ layout | string | 页面内layout布局数据 |
+++ node_id | int | 和para_node中的node_id对应 |
+++ box | list |
边框数据 「x, y, w, h」(x, y)为坐标点坐标,w为box宽度,h为box高度(以页面坐标为原点),版式格式时有效 |
+++ type | string | 布局类型 「text」- 段落、「image」- 图片、「table」- 表格、「head_tail」- 页眉页脚、「contents」- 目录、「cell」- 单元格(仅表格内才有) |
+++ text | string | 布局内文字信息 |
+++ children | list |
布局嵌套数据, 当前layout type为table时有值,列表内部数据结构同layout ,为单元格的内容 |
++++ box | list |
边框数据 「x, y, w, h」(x, y)为坐标点坐标,w为box宽度,h为box高度(以页面坐标为原点),版式格式时有效 |
++++ type | string | 「cell」- 单元格(仅表格内才有) |
++++ text | string | 表格单元格内文字 |
+++ matrix | list |
二维数组 表示表格内部合并单元格信息,「table类型layout返回,见table layout示例」 |
+++ merge_table | string | 表格结构才有该字段,「begin」- 跨页表格开始、「inner」- 跨页表格中间表格(表格跨页超过两页)、「end」- 跨页表格结束 |
++ sheet_name | string | excel sheet表名 |
++ type | string | 页面属性 「text」- 正文、「contents」- 目录、「appendix」- 附录、「others」- 其他 |
para_nodes | list |
文章段落标题层级结构树,「return_para_nodes=True」时返回 |
+ node_id | int | 节点id(从1开始计数,存在root节点id为0) |
+ text | string | 节点对应文本内容 |
+ node_type | string | 节点类型 样举值「root、title、text、image、table、head_tail、contents」 |
+ parent | int | 最近父节点node_id |
+ children | list |
子节点 node_id 数组 |
+ para_type | string | 标题类型,当nodetype为title时,固定格式 title{int}(如title_1、title_2)对应标题层数,其余情况同node_type |
+ position | list |
节点对应在文档中的位置信息,包含layout的位置信息。列表形式,每个元素为一个layout的位置信息 |
++ pageno | int | 对应文档页码 |
++ layout_index | int | layout在文档当前页中的索引 |
++ box | list |
layout在文档当前页中的外接矩形的坐标[x, y, w, h] |
返回示例
成功返回示例:
JSON
1{
2 "log_id": "23596597899286921761579365582373",
3 "error_code": 0,
4 "error_msg": "",
5 "result":
6 {
7 "task_id": "task-UnvGsgbYZp9pS3BZRHn11ifzjNvKzTgf",
8 "status": "success",
9 "task_error": null,
10 "duration": 902.0,
11 "parse_result_url": "https:xxxxxxxxxxxxxxxxxxx"
12 }
13}
解析结果示例:
JSON
1{
2 "file_name": "示例文件.pdf",
3 "para_nodes": [
4 {
5 "node_id": 0,
6 "text": "",
7 "node_type": "root",
8 "parent": null,
9 "children": [1],
10 "para_type": "root",
11 "position": []
12 },
13 {
14 "node_id": 1,
15 "text": "建构大模型智能审查方案",
16 "node_type": "title",
17 "parent": 0,
18 "children": [2],
19 "para_type": "title_1",
20 "position": [
21 {
22 "pageno": 0,
23 "layout_index": 0,
24 "box": [164, 115, 288, 28]
25 }
26 ]
27 },
28 {
29 "node_id": 2,
30 "text": "阿德勒的主张",
31 "node_type": "title",
32 "parent": 1,
33 "children": [3],
34 "para_type": "title_2",
35 "position": [
36 {
37 "pageno": 0,
38 "layout_index": 1,
39 "box": [79, 175, 271, 15]
40 }
41 ]
42 },
43 {
44 "node_id": 3,
45 "text": "阿德勒是个体心理学的创始人,他的理论强调个体在社会关系中的自我提升和归属感。",
46 "node_type": "text",
47 "parent": 2,
48 "children": [],
49 "para_type": "text",
50 "position": [
51 {
52 "pageno": 0,
53 "layout_index": 2,
54 "box": [79, 224, 441, 44]
55 }
56 ]
57 }
58 ],
59 "file_content": [
60 {
61 "page_num": 0,
62 "page_size": {
63 "width": 612,
64 "height": 792
65 },
66 "page_angle": 0,
67 "is_scan": false,
68 "page_content": {
69 "layout": [
70 {
71 "box": [164, 115, 288, 28],
72 "type": "text",
73 "text": "建构大模型智能审查方案",
74 "node_id": 1
75 },
76 {
77 "box": [79, 175, 271, 15],
78 "type": "text",
79 "text": "阿德勒的主张",
80 "node_id": 2
81 },
82 {
83 "box": [79, 224, 441, 44],
84 "type": "text",
85 "text": "阿德勒是个体心理学的创始人,他的理论强调个体在社会关系中的自我提升和归属感。",
86 "node_id": 3
87 }
88 ]
89 }
90 }
91 ]
92}
失败返回示例(详细的错误码说明见API文档-错误码):
JSON
1 {"log_id": "13665091038742503867108513247608",
2 "error_code": "282007",
3 "error_msg": "task not exist, please check task id",
4 "result": "null"}