如何利用Google Document AI从薪资单提取键值对及客户分块
用Google Document AI解析多客户扫描薪资单的最优方案
第一步:选对工具类型
单页多客户的薪资单属于重复结构化条目文档,别用基础OCR,直接上Google Document AI的「自定义文档解析器(Custom Document Parser)」——比表单解析器(Form Parser)更适合处理这种区块重复的场景,能精准识别每个客户的独立区块和内部字段。
第二步:训练自定义解析模型
- 标注样本:用Document AI Workbench的可视化标注工具,把示例薪资单里每个客户的区域框选出来,定义为「Customer Block」实体;再在每个区块内标注「客户名称」「应发薪资」「扣款」等子字段。如果只有这一种格式,标注10-20张样本足够让模型稳定识别。
- 训练迭代:上传标注好的数据集启动训练,训练完成后用未标注的薪资单测试,要是有识别不准的地方,补充标注或者调整实体定义,反复迭代到准确率达标。
第三步:后处理生成键值结构
模型返回的结果会包含所有识别出的客户区块和字段,写个简单的Python脚本就能把数据转换成「客户名称: {字段: 值}」的键值结构:
from google.cloud import documentai_v1 as documentai def parse_salary_sheet(project_id, location, processor_id, file_path): client = documentai.DocumentProcessorServiceClient() processor_name = client.processor_path(project_id, location, processor_id) with open(file_path, "rb") as f: image_content = f.read() raw_doc = documentai.RawDocument(content=image_content, mime_type="image/png") request = documentai.ProcessRequest(name=processor_name, raw_document=raw_doc) result = client.process_document(request=request) doc = result.document customer_map = {} for entity in doc.entities: if entity.type_ == "Customer Block": cust_name = None cust_details = {} for prop in entity.properties: if prop.type_ == "Customer Name": cust_name = prop.text_anchor.content.strip() else: cust_details[prop.type_] = prop.text_anchor.content.strip() if cust_name: customer_map[cust_name] = cust_details return customer_map
第四步:导出到电子表格
把键值结构转成表格格式,用pandas直接导出成Excel/CSV,或者用Google Sheets API同步到云端表格:
import pandas as pd # 调用上面的解析函数 salary_data = parse_salary_sheet("你的项目ID", "区域", "解析器ID", "薪资单图片路径") # 转成DataFrame df = pd.DataFrame.from_dict(salary_data, orient="index").reset_index() df.rename(columns={"index": "客户名称"}, inplace=True) # 导出Excel df.to_excel("薪资单数据.xlsx", index=False)
额外小技巧
如果薪资单格式完全固定,也可以先用Document AI的OCR提取全量文本,再用正则匹配每行客户数据,但这种方法容错率低——只要格式微调就会失效,还是训练自定义模型的方案更靠谱、更灵活。
内容的提问来源于stack exchange,提问作者SAMC3523
相关产品推荐
相关产品推荐

