使用 Python 从发票中提取数据

本教程指导如何使用 Python 从发票中提取数据。它包含设置开发 IDE 的所有详细信息、定义程序流程的步骤列表以及演示使用 Python 的发票 OCR 软件的示例代码。您将学习如何根据您的要求从 PNGJPEG、BMP、TIFF 和 GIF 等图像中自定义检测过程。

使用 Python 进行发票 OCR 的步骤

  1. 设置环境以使用 Aspose.OCR for Python via .NET 提取发票数据
  2. 创建 Aspose.Ocr 实例以进行 OCR 处理
  3. 创建 OcrInput 类的实例,用于保存收据
  4. 将收据添加到 OcrInput 集合
  5. 设置收据识别设置并设置识别语言
  6. 使用 recognize_receipt 方法执行 OCR,识别输入收据中的文本
  7. 显示收据中识别的文本

这些步骤描述了如何使用 Python 应用 OCR 来处理收据。创建 Aspose.Ocr 对象的实例,初始化用于保存收据的 OcrInput 对象,并创建 ReceiptRecognitionSettings 对象来定义发票 OCR 的参数。最后,通过提供收据列表和用于提取文本的设置来调用 understand_receipt() 方法。

使用 Python 提取发票数据的代码

import aspose.ocr as api
from aspose.ocr import License
# Instantiate and apply the license for Aspose.OCR to enable full functionality.
license = License()
license.set_license("License.lic")
# Create an instance of the Aspose.Ocr class for OCR processing.
extractTextFromReceipt = api.AsposeOcr()
# Initialize an OcrInput object to hold input image(s) for OCR processing.
receiptDatas = api.OcrInput(api.InputType.SINGLE_IMAGE)
# Add images (receipts) to the OcrInput object for recognition.
receiptDatas.add("Receipt1.png")
receiptDatas.add("Receipt2.png")
# Set up receipt recognition settings.
recognitionSettings = api.ReceiptRecognitionSettings()
recognitionSettings.language = api.Language.ENG # Specify the language as English.
# Perform OCR to recognize text from the input receipts using the specified settings.
results = extractTextFromReceipt.recognize_receipt(receiptDatas, recognitionSettings)
# Get the number of recognized results (one result per input image).
length = results.length
# Loop through each result and print the recognized text for each input image.
for i in range(length):
print(results[i].recognition_text)

此示例代码演示了如何使用 Python 的 发票 OCR API。您可以将输入类型设置为 PDF、TIFF、URL、目录、Zip 等,并从语言枚举器中的大量语言名称列表中设置检测语言,ReceiptRecognitionSettings 类包含许多属性,例如设置允许的字符集、设置自动颜色反转的标志以及定义要忽略的字符黑名单。

本文教我们提取发票文本的过程。要将手写文本转换为可编辑和可搜索的文本,请参阅 使用 Python 将手写内容转换为文本 上的文章。

 简体中文