OCR 与文档分析提示
从文档图像中提取文本、表格和结构
OCR 与文档分析提示 是 CoddyKit 上的免费 AI Prompt Engineering 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AI Prompt Engineering 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AI Prompt Engineering 课程共包含 4 节课。
将 LLM 用作文档阅读器
OCR(光学字符识别)过去通常需要专用软件才能从图片中提取文字。现在,视觉 LLM 不仅可以读取图片中的文字,还能理解内容——不只是提取字符,还能解析结构、表格、手写内容和上下文。
常见的文档分析任务:
- 从扫描文档中提取文字
- 读取收据、发票和表单
- 解析表格和图表
- 转录手写笔记
- 读取印刷标签和标牌
基本文字提取提示词
从文档图片中进行简单的文字提取:
import anthropic, base64
client = anthropic.Anthropic(api_key='YOUR_API_KEY')
def extract_text(image_path, extraction_prompt):
with open(image_path, 'rb') as f:
img_b64 = base64.standard_b64encode(f.read()).decode('utf-8')
r = client.messages.create(
model='claude-opus-4-5', max_tokens=1000,
messages=[{'role': 'user', 'content': [
{'type': 'image', 'source': {'type': 'base64', 'media_type': 'image/jpeg', 'data': img_b64}},
{'type': 'text', 'text': extraction_prompt}
]}]
)
return r.content[0].text
# Basic extraction
basic_prompt = 'Extract all text from this document image exactly as it appears. Preserve line breaks.'
# Structure-preserving extraction
structured_prompt = 'Extract all text from this document image. Preserve: paragraph structure, line breaks, and any visible formatting. Do not add any text not present in the image.'
print('Text extraction functions defined.')保留表格结构
当文档包含表格时,将其提取为纯文本会丢失结构。请使用明确要求保留表格格式的提示词:
table_prompt = '''
Extract all text from this document image.
If the document contains any tables, preserve the table structure using markdown table format:
| Column 1 | Column 2 | Column 3 |
|----------|----------|----------|
| Value | Value | Value |
For any text outside tables, use plain text preserving paragraph structure.
Do not invent or infer any data not visible in the image.
'''
# For structured output:
table_json_prompt = '''
Extract the table from this image.
Return JSON:
{
"headers": ["column name"],
"rows": [["cell value", "cell value"]],
"caption": "table caption if present or null"
}
If a cell is empty or illegible, use null.
'''
print('Table extraction prompts defined.')收据明细提取
收据具有特定结构——表头(商户)、明细行和总计。专门针对收据的提示词可以可靠地提取这一结构:
import json
receipt_prompt = '''
Extract all information from this receipt image.
Return JSON:
{
"merchant": {
"name": str,
"address": str or null,
"phone": str or null
},
"transaction": {
"date": "YYYY-MM-DD or as written",
"time": "HH:MM or as written or null",
"receipt_number": str or null,
"payment_method": str or null
},
"items": [
{"description": str, "quantity": number or null, "unit_price": number or null, "total": number}
],
"subtotal": number or null,
"tax": number or null,
"tip": number or null,
"total": number,
"currency": "3-letter ISO code"
}
For any field not visible, use null. For numbers, use numeric type (not string).
'''
def extract_receipt(image_path):
text = extract_text(image_path, receipt_prompt)
return json.loads(text)
print('Receipt extraction function defined.')手写笔记转录
转录手写内容时,提示词需要说明其中的难点——无法辨认的词、划掉的文字和缩写:
handwriting_prompt = '''
Transcribe the handwritten text in this image as accurately as possible.
Handling rules:
- If a word is illegible, write [ILLEGIBLE]
- If a word is partially legible, write [PARTIAL: best_guess]
- If text is crossed out, include it with strikethrough notation: ~~crossed out text~~
- Preserve line breaks as they appear
- If there are arrows, circles, or annotations, note them in brackets: [arrow pointing right]
- Do not correct spelling or grammar
After transcription, estimate overall legibility: high (>90% readable) | medium (70-90%) | low (<70%)
Format:
TRANSCRIPTION:
[transcribed text here]
LEGIBILITY: [rating]
'''
print(handwriting_prompt)表单字段提取
印刷表单包含带标签的字段和填写的值。表单提取提示词会将标签映射到对应的值:
form_prompt = '''
Extract all form fields and their values from this document image.
For each field:
- Field label: the printed label (e.g., "First Name:", "Date of Birth:")
- Field value: the filled-in value (handwritten or typed)
- Filled: whether the field has been filled in (true/false)
Return JSON:
{
"form_title": str or null,
"fields": [
{
"label": str,
"value": str or null,
"filled": true | false
}
],
"signature_present": true | false,
"date_signed": str or null
}
If the value is illegible, use "[ILLEGIBLE]".
If the field is blank, value should be null and filled should be false.
'''
print('Form field extraction prompt defined.')
print('Handles: printed forms, questionnaires, applications.')提取前的文档分类
在提取前加入分类步骤,以便针对每种文档类型应用正确的提取模式:
import json
DOC_SCHEMAS = {
'receipt': receipt_prompt,
'form': form_prompt,
'table': table_json_prompt,
'letter': 'Extract all text preserving paragraph structure. Identify: sender, recipient, date, subject, body.',
'label': 'Extract all text from this label. Include: product name, ingredients/contents, weight, expiry date, barcode numbers.'
}
def classify_and_extract(image_path):
# Step 1: Classify document type
classify_prompt = 'What type of document is this? Return JSON: {"type": "receipt|form|table|letter|label|other", "confidence": "high|medium|low"}'
classification_text = extract_text(image_path, classify_prompt)
doc_type = json.loads(classification_text)['type']
# Step 2: Apply correct schema
schema = DOC_SCHEMAS.get(doc_type, 'Extract all visible text from this document.')
extracted = extract_text(image_path, schema)
return {'type': doc_type, 'data': extracted}
print('Document classify-then-extract pipeline defined.')低质量图片处理
并非所有文档图片都清晰。提示词应能妥善处理质量下降的图片:
low_quality_prompt = '''
Extract text from this document image. The image may be low quality, blurry, or poorly lit.
Extraction guidelines:
- Extract all text you can read with reasonable confidence
- For unclear sections, use [UNCLEAR] as a placeholder
- For completely unreadable sections, use [UNREADABLE: approximately N words]
- Do not guess or hallucinate words you cannot see clearly
- Note image quality issues at the end: "Image quality: [good/fair/poor]. Issues: [description]"
Be conservative — it is better to mark something as unclear than to guess incorrectly.
'''
print(low_quality_prompt)
print('\nConservative approach: unclear beats hallucinated.')多页文档摘要
对于以多张图片形式发送的多页文档,应将逐页提取与综合步骤结合起来:
def extract_multi_page_document(image_paths):
# Step 1: Extract text from each page
page_texts = []
for i, path in enumerate(image_paths):
page_text = extract_text(path, f'Extract all text from page {i+1} of this document. Preserve structure.')
page_texts.append(f'=== PAGE {i+1} ===\n{page_text}')
full_text = '\n\n'.join(page_texts)
# Step 2: Synthesize summary and key information
r = client.messages.create(
model='claude-opus-4-5', max_tokens=500,
messages=[{'role': 'user', 'content': f'''
Here is the extracted text from a {len(image_paths)}-page document:\n\n{full_text}\n\n
Provide:
1. Document type and title
2. 3-sentence summary
3. Key data points extracted
Return JSON: {{"type": str, "title": str, "summary": str, "key_data": [str]}}
'''}]
)
return json.loads(r.content[0].text)
print('Multi-page document pipeline defined.')OCR 提取后的验证
在将 OCR 输出用于后续系统之前,必须对其进行验证。常见的验证检查包括:
import re
from datetime import datetime
def validate_receipt_extraction(data):
errors = []
# Validate total is present and numeric
if data.get('total') is None:
errors.append('total is missing')
elif not isinstance(data['total'], (int, float)):
errors.append(f'total is not numeric: {data["total"]}')
# Validate date format
if data.get('transaction', {}).get('date'):
date_str = data['transaction']['date']
try:
datetime.strptime(date_str, '%Y-%m-%d')
except ValueError:
errors.append(f'date format invalid: {date_str}')
# Validate line items total approximately equals subtotal
if data.get('items') and data.get('subtotal'):
items_total = sum(item.get('total', 0) for item in data['items'] if item.get('total'))
if abs(items_total - data['subtotal']) > 0.05:
errors.append(f'Items total {items_total} does not match subtotal {data["subtotal"]}')
return errors
print('Receipt validation function defined.')从图表和曲线图中提取结构化数据
文档图片中的图表和曲线图包含传统 OCR 无法识别、但视觉 LLM 可以读取的数据。图表提取提示词会要求模型读取底层数据值:
chart_prompt = '''
Extract the data from this chart or graph image.
Identify:
1. Chart type (bar, line, pie, scatter, table)
2. Title and axis labels
3. All data series names
4. All data points with their labels/values
5. Any notable trend or pattern
Return JSON:
{
"chart_type": str,
"title": str or null,
"x_axis_label": str or null,
"y_axis_label": str or null,
"data_series": [
{"name": str, "values": [{"label": str, "value": number}]}
],
"key_insight": str
}
If exact values are not readable, provide best estimates with a note.
'''
print(chart_prompt)快速检查
从图片中转录手写内容时,对于无法辨认的词,建议采用什么方法?
OCR 与文档分析——要点总结
视觉 LLM 提供了超越字符识别的灵活文档分析能力:
- 基本提取:保留换行和段落结构;请明确提出这一要求
- 表格:使用标记语言表格格式,或使用包含行和表头的对象表示法模式来保留结构
- 收据:使用包含商户、商品和总计字段的专用模式
- 手写内容:对无法读取的内容使用 [ILLEGIBLE],对部分可读的内容使用 [PARTIAL]——绝不要猜测
- 表单:建立标签到值的对应关系;为每个字段标明已填写或未填写状态
- 先对文档类型进行分类,再应用相应的提取模式
- 通过程序验证提取的数据:必填字段、数值类型、日期格式和数学校验
- 低质量图片:保守提取胜过自信地编造内容
用 AI 导师学习 AI Prompt Engineering — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 53
- 课程
- 199
常见问题解答
「OCR 与文档分析提示」课时是免费的吗?
是的 — 「OCR 与文档分析提示」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AI Prompt Engineering 课程的其余内容,请升级到 CoddyKit PRO。 AI Prompt Engineering 课程共包含 4 节课。
「OCR 与文档分析提示」这节课中我会学到什么?
从文档图像中提取文本、表格和结构 你通过在浏览器中直接运行的动手代码来练习 AI Prompt Engineering,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 AI Prompt Engineering 需要有经验吗?
无需任何先前经验。CoddyKit 上的 AI Prompt Engineering 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「OCR 与文档分析提示」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 AI Prompt Engineering 课中编写并运行代码吗?
能。每节 AI Prompt Engineering 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。