You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Azure Form Recognizer Python SDK返回结果保存为API同款JSON文件

Form Recognizer Python SDK结果转API一致JSON的实现方法

azure.ai.formrecognizer库返回的所有专属数据类型,都内置了to_dict()方法,调用后会输出和REST API返回结构完全一致的Python原生字典,直接对字典做JSON序列化即可得到和API调用结果相同的JSON文件。

完整实现代码如下:

import os
import json
from azure.ai.formrecognizer import FormRecognizerClient
from azure.identity import ClientSecretCredential

# 初始化客户端
client_secret_credential = ClientSecretCredential(tenant_id, client_id, client_secret)
form_recognizer_client = FormRecognizerClient(endpoint, client_secret_credential)

# 识别文件内容
with open(os.path.join(path, file_name), "rb") as fd:
    form = fd.read()
poller = form_recognizer_client.begin_recognize_content(form)
form_pages = poller.result()

# 转换为与API结构一致的字典并保存为JSON
form_result = [page.to_dict() for page in form_pages]
with open("recognize_result.json", "w", encoding="utf-8") as f:
    json.dump(form_result, f, ensure_ascii=False, indent=2)

补充说明:

  • 该方法对所有Form Recognizer识别能力都适用,包括自定义模型识别、预训练发票/收据/身份证识别等,只要对返回的结果对象调用to_dict()即可
  • 若使用v3及以上版本的Form Recognizer SDK,接口返回的AnalyzeResult对象同样支持to_dict()方法,用法完全一致

内容的提问来源于stack exchange,提问作者isacontreras

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 19:15:07