本页面介绍了如何开始使用 Document AI Toolbox API 的 Cloud 客户端库。通过客户端库,您可以更轻松地使用支持的语言访问Google Cloud API。虽然您可以通过向服务器发出原始请求来直接使用Google Cloud API,但客户端库可实现简化,从而显著减少您需要编写的代码量。
请参阅客户端库说明,详细了解 Cloud 客户端库和旧版 Google API 客户端库。
安装客户端库
Python
pip install --upgrade google-cloud-documentai-toolbox
如需了解详情,请参阅设置 Python 开发环境。
设置身份验证
为了对 Google Cloud API 的调用进行身份验证,客户端库支持应用默认凭证 (ADC);这些库会在一组指定的位置查找凭证,并使用这些凭证对发送到 API 的请求进行身份验证。借助 ADC,您可以在各种环境(例如本地开发或生产环境)中为您的应用提供凭据,而无需修改应用代码。对于生产环境,设置 ADC 的方式取决于服务和上下文。如需了解详情,请参阅设置应用默认凭证。
对于本地开发环境,您可以使用与您的 Google 账号关联的凭据设置 ADC:
-
Install the Google Cloud CLI. After installation, initialize the Google Cloud CLI by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
登录屏幕随即出现。在您登录后,您的凭据会存储在 ADC 使用的本地凭据文件中。
使用客户端库
Document AI Toolbox 是一款适用于 Python 的 SDK,可提供实用函数来管理、操控和提取文档响应中的信息。它会从 Cloud Storage 中的 JSON 文件、本地 JSON 文件或直接从 process_document()
方法输出的处理后的文档响应中创建“封装”文档对象。
它可以执行以下操作:
- 将批处理中的碎片化
Document
JSON 文件合并为单个“封装”文档。 - 将分片导出为统一的
Document
。 -
从以下位置获取
Document
输出: - 无需处理
Layout
信息,即可访问Pages
、Lines
、Paragraphs
、FormFields
和Tables
中的文本。 - 搜索包含目标字符串或与正则表达式匹配的
Pages
。 - 按名称搜索
FormFields
。 - 按类型搜索
Entities
。 - 将
Tables
转换为 Pandas DataFrame 或 CSV。 - 将
Entities
和FormFields
插入到 BigQuery 表中。 - 根据 Splitter/Classifier 处理器的输出拆分 PDF 文件。
- 从
Document
边界框中提取图片Entities
。 -
将
Documents
转换为常用格式,以及从常用格式转换为Documents
:- Cloud Vision API
AnnotateFileResponse
- hOCR
- 第三方文档处理格式
- Cloud Vision API
- 从 Cloud Storage 文件夹创建批次文档以进行处理。
代码示例
以下代码示例演示了如何使用 Document AI 工具箱。
快速入门
表
BigQuery Export
PDF 拆分
图片提取
视觉转换
hOCR 转换
第三方转化
文档批次
合并文档分片
其他资源
Python
以下列表包含与 Python 版客户端库相关的更多资源的链接: