Documentation
How to extract information from Payslip using SoceTonAI Script OCR¶
In this guide, I’ll show how to use SoceTonAI Script OCR to automatically extract key information from a Payslip. We begin by collecting the API credentials from soceton.com, then create a YAML script that instructs the OCR engine to read the information we need (YAML is provided at the end of this document). Finally, we send a read request to api.soceton.com to retrieve the structured output.
YAML that I am using for this request¶
document_type: Payslip
description: "Extract key information from Payslip using OCR"
development: true
fields:
- name: employee_name
label: "Employee Name"
find:
type: text
keywords:
- keyword: "Employee"
index: 1
next_keyword_position: [ 1, -1, 1, 10 ]
- keyword: "Name"
position_of_value: [1, -1, 10, 18]
- name: employee_id
label: "Employee ID"
find:
type: text
keywords:
- keyword: "Employee"
index: 2
next_keyword_position: [ 1, -1, 1, 10 ]
- keyword: "ID"
position_of_value: [1, -1, 10, 18]
- name: department
label: "Department"
find:
type: text
keywords:
- keyword: "Department"
index: 0
position_of_value: [1, -1, 15, 40]
- name: designation
label: "Designation"
find:
type: text
keywords:
- keyword: "Designation"
index: 0
position_of_value: [1, -1, 15, 40]
- name: pay_period
label: "Pay Period"
find:
type: text
keywords:
- keyword: "Pay"
index: 0
next_keyword_position: [ 1, -1, 1, 10 ]
- keyword: "Period"
position_of_value: [1, -1, 6, 15]
- name: date_issued
label: "Date Issued"
find:
type: text
keywords:
- keyword: "Date"
index: 0
next_keyword_position: [ 1, -1, 1, 10 ]
- keyword: "Issued"
position_of_value: [1, -1, 6, 15]
- name: total_earnings
label: "Total Earnings"
find:
type: text
keywords:
- keyword: "Total"
index: 0
next_keyword_position: [ 1, -1, 1, 10 ]
- keyword: "Earnings"
position_of_value: [1, -1, 15, 35]
- name: total_deductions
label: "Total Deductions"
find:
type: text
keywords:
- keyword: "Total"
index: 1
next_keyword_position: [ 1, -1, 1, 15 ]
- keyword: "Deductions"
position_of_value: [1, -1, 15, 30]
- name: net_salary
label: "Net Salary"
find:
type: text
keywords:
- keyword: "Net"
index: 1
next_keyword_position: [ 1, -1, 1, 10 ]
- keyword: "Salary"
position_of_value: [1, -1, 15, 30]
The image¶
In [1]:
from PIL import Image
img = Image.open("dummies/images/payslip-1_0.jpg")
img = img.convert("RGB")
img
Out[1]:
Sending the request¶
In [2]:
import json
import requests
from config import SOCETONAI_API_SECRET, SOCETONAI_API_KEY
def generate_result(url, image_path, rules_path, headers):
data = {}
files = {
"doc": open(image_path, "rb"),
"rules": open(rules_path, "r", encoding="utf-8")
}
response = requests.post(url, data=data, headers=headers, files=files)
return response
result = generate_result(
"https://api.soceton.com/script-ocr/read",
"dummies/images/payslip-1_0.jpg",
"dummies/ymls/payslip-1_0.yml", {
"X-API-KEY": SOCETONAI_API_KEY,
"X-API-SECRET": SOCETONAI_API_SECRET
})
result = result.json()
values = {}
for k in result["result"].keys():
try:
values[k] = result["result"][k]
except Exception as e:
print(k, ":", e)
print(json.dumps(values, indent=4))
{
"employee_name": "John Doe",
"employee_id": "EMP - 10234",
"department": "Software Development",
"designation": "Senior Developer",
"pay_period": "January 2025",
"date_issued": "Feb 1 , 2025",
"total_earnings": "$ 4,650.00",
"total_deductions": "$ 650.00",
"net_salary": "$ 4,000.00"
}
Printing the values¶
In [3]:
print(json.dumps(result, indent=4))
{
"success": true,
"result": {
"employee_name": "John Doe",
"employee_id": "EMP - 10234",
"department": "Software Development",
"designation": "Senior Developer",
"pay_period": "January 2025",
"date_issued": "Feb 1 , 2025",
"total_earnings": "$ 4,650.00",
"total_deductions": "$ 650.00",
"net_salary": "$ 4,000.00"
}
}