Skip to content

Commit 708bca1

Browse files
authored
add check before printing receipt items (#19342)
* add check before printing receipt items * add check before printing receipt items
1 parent f90c95b commit 708bca1

File tree

4 files changed

+64
-60
lines changed

4 files changed

+64
-60
lines changed

sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_async.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,22 @@ async def recognize_receipts(self):
6161
transaction_date = receipt.fields.get("TransactionDate")
6262
if transaction_date:
6363
print("Transaction Date: {} has confidence: {}".format(transaction_date.value, transaction_date.confidence))
64-
print("Receipt items:")
65-
for idx, item in enumerate(receipt.fields.get("Items").value):
66-
print("...Item #{}".format(idx+1))
67-
item_name = item.value.get("Name")
68-
if item_name:
69-
print("......Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence))
70-
item_quantity = item.value.get("Quantity")
71-
if item_quantity:
72-
print("......Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence))
73-
item_price = item.value.get("Price")
74-
if item_price:
75-
print("......Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence))
76-
item_total_price = item.value.get("TotalPrice")
77-
if item_total_price:
78-
print("......Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence))
64+
if receipt.fields.get("Items"):
65+
print("Receipt items:")
66+
for idx, item in enumerate(receipt.fields.get("Items").value):
67+
print("...Item #{}".format(idx+1))
68+
item_name = item.value.get("Name")
69+
if item_name:
70+
print("......Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence))
71+
item_quantity = item.value.get("Quantity")
72+
if item_quantity:
73+
print("......Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence))
74+
item_price = item.value.get("Price")
75+
if item_price:
76+
print("......Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence))
77+
item_total_price = item.value.get("TotalPrice")
78+
if item_total_price:
79+
print("......Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence))
7980
subtotal = receipt.fields.get("Subtotal")
8081
if subtotal:
8182
print("Subtotal: {} has confidence: {}".format(subtotal.value, subtotal.confidence))

sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_from_url_async.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,22 @@ async def recognize_receipts_from_url(self):
5757
transaction_date = receipt.fields.get("TransactionDate")
5858
if transaction_date:
5959
print("Transaction Date: {} has confidence: {}".format(transaction_date.value, transaction_date.confidence))
60-
print("Receipt items:")
61-
for idx, item in enumerate(receipt.fields.get("Items").value):
62-
print("...Item #{}".format(idx+1))
63-
item_name = item.value.get("Name")
64-
if item_name:
65-
print("......Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence))
66-
item_quantity = item.value.get("Quantity")
67-
if item_quantity:
68-
print("......Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence))
69-
item_price = item.value.get("Price")
70-
if item_price:
71-
print("......Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence))
72-
item_total_price = item.value.get("TotalPrice")
73-
if item_total_price:
74-
print("......Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence))
60+
if receipt.fields.get("Items"):
61+
print("Receipt items:")
62+
for idx, item in enumerate(receipt.fields.get("Items").value):
63+
print("...Item #{}".format(idx+1))
64+
item_name = item.value.get("Name")
65+
if item_name:
66+
print("......Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence))
67+
item_quantity = item.value.get("Quantity")
68+
if item_quantity:
69+
print("......Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence))
70+
item_price = item.value.get("Price")
71+
if item_price:
72+
print("......Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence))
73+
item_total_price = item.value.get("TotalPrice")
74+
if item_total_price:
75+
print("......Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence))
7576
subtotal = receipt.fields.get("Subtotal")
7677
if subtotal:
7778
print("Subtotal: {} has confidence: {}".format(subtotal.value, subtotal.confidence))

sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,22 @@ def recognize_receipts(self):
5858
transaction_date = receipt.fields.get("TransactionDate")
5959
if transaction_date:
6060
print("Transaction Date: {} has confidence: {}".format(transaction_date.value, transaction_date.confidence))
61-
print("Receipt items:")
62-
for idx, item in enumerate(receipt.fields.get("Items").value):
63-
print("...Item #{}".format(idx+1))
64-
item_name = item.value.get("Name")
65-
if item_name:
66-
print("......Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence))
67-
item_quantity = item.value.get("Quantity")
68-
if item_quantity:
69-
print("......Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence))
70-
item_price = item.value.get("Price")
71-
if item_price:
72-
print("......Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence))
73-
item_total_price = item.value.get("TotalPrice")
74-
if item_total_price:
75-
print("......Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence))
61+
if receipt.fields.get("Items"):
62+
print("Receipt items:")
63+
for idx, item in enumerate(receipt.fields.get("Items").value):
64+
print("...Item #{}".format(idx+1))
65+
item_name = item.value.get("Name")
66+
if item_name:
67+
print("......Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence))
68+
item_quantity = item.value.get("Quantity")
69+
if item_quantity:
70+
print("......Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence))
71+
item_price = item.value.get("Price")
72+
if item_price:
73+
print("......Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence))
74+
item_total_price = item.value.get("TotalPrice")
75+
if item_total_price:
76+
print("......Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence))
7677
subtotal = receipt.fields.get("Subtotal")
7778
if subtotal:
7879
print("Subtotal: {} has confidence: {}".format(subtotal.value, subtotal.confidence))

sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts_from_url.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,22 @@ def recognize_receipts_from_url(self):
5656
transaction_date = receipt.fields.get("TransactionDate")
5757
if transaction_date:
5858
print("Transaction Date: {} has confidence: {}".format(transaction_date.value, transaction_date.confidence))
59-
print("Receipt items:")
60-
for idx, item in enumerate(receipt.fields.get("Items").value):
61-
print("...Item #{}".format(idx+1))
62-
item_name = item.value.get("Name")
63-
if item_name:
64-
print("......Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence))
65-
item_quantity = item.value.get("Quantity")
66-
if item_quantity:
67-
print("......Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence))
68-
item_price = item.value.get("Price")
69-
if item_price:
70-
print("......Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence))
71-
item_total_price = item.value.get("TotalPrice")
72-
if item_total_price:
73-
print("......Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence))
59+
if receipt.fields.get("Items"):
60+
print("Receipt items:")
61+
for idx, item in enumerate(receipt.fields.get("Items").value):
62+
print("...Item #{}".format(idx+1))
63+
item_name = item.value.get("Name")
64+
if item_name:
65+
print("......Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence))
66+
item_quantity = item.value.get("Quantity")
67+
if item_quantity:
68+
print("......Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence))
69+
item_price = item.value.get("Price")
70+
if item_price:
71+
print("......Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence))
72+
item_total_price = item.value.get("TotalPrice")
73+
if item_total_price:
74+
print("......Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence))
7475
subtotal = receipt.fields.get("Subtotal")
7576
if subtotal:
7677
print("Subtotal: {} has confidence: {}".format(subtotal.value, subtotal.confidence))

0 commit comments

Comments
 (0)