-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathsample_analyze_bank_statement.py
More file actions
175 lines (150 loc) · 7.2 KB
/
sample_analyze_bank_statement.py
File metadata and controls
175 lines (150 loc) · 7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# coding: utf-8
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
"""
This code sample shows Prebuilt US Bank Statement operations with the Azure AI Document Intelligence client library.
The async versions of the samples require Python 3.8 or later.
To learn more, please visit the documentation - Quickstart: Document Intelligence (formerly Form Recognizer) SDKs
https://learn.microsoft.com/azure/ai-services/document-intelligence/quickstarts/get-started-sdks-rest-api?pivots=programming-language-python
"""
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence import DocumentIntelligenceClient
from dotenv import find_dotenv, load_dotenv
"""
Remember to remove the key from your code when you're done, and never post it publicly. For production, use
secure methods to store and access your credentials. For more information, see
https://docs.microsoft.com/en-us/azure/cognitive-services/cognitive-services-security?tabs=command-line%2Ccsharp#environment-variables-and-application-configuration
"""
load_dotenv(find_dotenv())
endpoint = os.environ["DOCUMENTINTELLIGENCE_ENDPOINT"]
key = os.environ["DOCUMENTINTELLIGENCE_API_KEY"]
filepath = "YOUR_FILE_PATH"
document_intelligence_client = DocumentIntelligenceClient(
endpoint=endpoint, credential=AzureKeyCredential(key)
)
with open(filepath, "rb") as f:
poller = document_intelligence_client.begin_analyze_document(
"prebuilt-bankStatement.us", body=f
)
bankstatements = poller.result()
for idx, statement in enumerate(bankstatements.documents):
print("--------Recognizing statement #{}--------".format(idx + 1))
account_holder_name = statement.fields.get("AccountHolderName")
if account_holder_name:
print(
"Account Holder Name: {} has confidence: {}".format(
account_holder_name.value_string, account_holder_name.confidence
)
)
account_holder_address = statement.fields.get("AccountHolderAddress")
if account_holder_address:
print(
"Account Holder Address: {} has confidence: {}".format(
account_holder_address.value_address, account_holder_address.confidence
)
)
bank_name = statement.fields.get("BankName")
if bank_name:
print(
"Bank Name: {} has confidence: {}".format(
bank_name.value_string, bank_name.confidence
)
)
bank_address = statement.fields.get("BankAddress")
if bank_address:
print(
"Bank Address: {} has confidence: {}".format(
bank_address.value_address, bank_address.confidence
)
)
statement_start_date = statement.fields.get("StatementStartDate")
if statement_start_date:
print(
"Statement Start Date: {} has confidence: {}".format(
statement_start_date.value_date, statement_start_date.confidence
)
)
statement_end_date = statement.fields.get("StatementEndDate")
if statement_end_date:
print(
"Statement End Date: {} has confidence: {}".format(
statement_end_date.value_date, statement_end_date.confidence
)
)
accounts = statement.fields.get("Accounts")
if accounts:
for account_idx, account in enumerate(accounts.value_array):
print("...Account #{}".format(account_idx + 1))
account_number = account.value_object.get("AccountNumber")
if account_number:
print(
"......Account Number: {} has confidence: {}".format(
account_number.value_string, account_number.confidence
)
)
account_type = account.value_object.get("AccountType")
if account_type:
print(
"......Account Type: {} has confidence: {}".format(
account_type.value_string, account_type.confidence
)
)
beginning_balance = account.value_object.get("BeginningBalance")
if beginning_balance:
print(
"......Beginning Balance: {} has confidence: {}".format(
beginning_balance.value_number, beginning_balance.confidence
)
)
ending_balance = account.value_object.get("EndingBalance")
if ending_balance:
print(
"......Ending Balance: {} has confidence: {}".format(
ending_balance.value_number, ending_balance.confidence
)
)
total_service_fees = account.value_object.get("TotalServiceFees")
if total_service_fees:
print(
"......Total Service Fees: {} has confidence: {}".format(
total_service_fees.value_number, total_service_fees.confidence
)
)
transactions = account.value_object.get("Transactions")
if transactions:
print("......Transactions:")
for transaction_idx, transaction in enumerate(transactions.value_array):
print(".........Transaction #{}".format(transaction_idx + 1))
transaction_date = transaction.value_object.get("Date")
if transaction_date:
print(
"............Date: {} has confidence: {}".format(
transaction_date.value_date, transaction_date.confidence
)
)
description = transaction.value_object.get("Description")
if description:
print(
"............Description: {} has confidence: {}".format(
description.value_string, description.confidence
)
)
deposit_amount = transaction.value_object.get("DepositAmount")
if deposit_amount:
print(
"............Deposit Amount: {} has confidence: {}".format(
deposit_amount.value_number, deposit_amount.confidence
)
)
withdrawal_amount = transaction.value_object.get("WithdrawalAmount")
if withdrawal_amount:
print(
"............Withdrawal Amount: {} has confidence: {}".format(
withdrawal_amount.value_number, withdrawal_amount.confidence
)
)
print("--------------------------------------")