本頁說明如何開始使用 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 initIf 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() 方法的輸出內容,建立「包裝」的文件物件。
可執行下列動作:
- 將批次處理產生的
DocumentJSON 檔案片段合併為單一「包裝」文件。 - 將分片匯出為統一的
Document。 -
從下列位置取得
Document輸出內容: - 存取
Pages、Lines、Paragraphs、FormFields和Tables中的文字,不必處理Layout資訊。 - 搜尋包含目標字串或符合規則運算式的
Pages。 - 依名稱搜尋
FormFields。 - 依類型搜尋「
Entities」。 - 將
Tables轉換為 Pandas DataFrame 或 CSV。 - 將
Entities和FormFields插入 BigQuery 資料表。 - 根據分割器/分類器處理器的輸出內容分割 PDF 檔案。
- 從
Document定界框中擷取圖片Entities。 -
將
Documents轉換為常用格式,或從常用格式轉換:- Cloud Vision API
AnnotateFileResponse - hOCR
- 第三方文件處理格式
- Cloud Vision API
- 從 Cloud Storage 資料夾建立要處理的文件批次。
程式碼範例
下列程式碼範例說明如何使用 Document AI Toolbox。
快速入門導覽課程
資料表
BigQuery 匯出內容
分割 PDF
圖片擷取
Vision 轉換
hOCR 轉換
第三方轉換
文件批次
合併文件分片
其他資源
Python
下方列出與 Python 用戶端程式庫相關的其他資源連結: