TL;DR
版本基线:2025-03-08;Claude 3.5 Sonnet;Python 3.11.8;Poppler 24.02;Pandoc 3.1.13。本文用于处理长PDF、Word、岗位说明书、设备SOP、候选人简历包。目标不是聊天,是把非结构化文档稳定转成可审计的表格。适用搜索意图:Claude长文本分析怎么用、Claude文档处理教程、Claude PDF分析、Claude下载替代方案、Google AI怎么用对比。
结论:先本地转纯文本,再按章节切块,再让Claude抽取JSON,最后用脚本校验字段。不要直接把80MB PDF丢进对话框。失败原因通常是OCR脏文本、表格断行、上下文超限、提示词没有字段约束。
Pre-requisites
准备目录。本文样例:30份制造业岗位JD,PDF总大小46.8MB,Word总大小18.2MB。我的测试机器:Ubuntu 22.04,4核8GB,家庭宽带下行300Mbps。
mkdir -p ~/claude-doc-lab/{input,txt,out} cd ~/claude-doc-lab # Expected output: # 当前目录切换到 /home/<user>/claude-doc-lab安装转换工具。Windows可用WSL2执行同样命令。
sudo apt-get update && sudo apt-get install -y poppler-utils pandoc python3-pip # Expected output: # Setting up poppler-utils ... # Setting up pandoc ... # Processing triggers completed确认版本。版本不一致时,先记录,不要混跑结果。
pdftotext -v 2>&1 | head -n 1 pandoc -v | head -n 1 python3 --version # Expected output: # pdftotext version 24.02.0 # pandoc 3.1.13 # Python 3.11.8
Note: Gemini怎么注册、Gemini国内使用、Google AI怎么用属于账号和访问层问题;本文只覆盖文档处理链路。Claude、Gemini、ChatGPT都应先吃干净文本,不应直接依赖网页上传后的黑盒解析。
Runbook:转换、切块、抽取、验证
PDF转文本。优先保留布局。扫描件先走OCR;可复制PDF直接用pdftotext。
find input -name "*.pdf" -print0 | while IFS= read -r -d '' f; do base=$(basename "$f" .pdf) pdftotext -layout "$f" "txt/${base}.txt" done ls -lh txt | head # Expected output: # -rw-r--r-- 1 user user 184K 2025-03-08 cnc_operator_jd.txt # -rw-r--r-- 1 user user 221K 2025-03-08 qa_engineer_resume.txtWord转Markdown。Markdown比纯文本更能保留标题层级,适合Claude长文本分析。
find input -name "*.docx" -print0 | while IFS= read -r -d '' f; do base=$(basename "$f" .docx) pandoc "$f" -t markdown -o "txt/${base}.md" done wc -c txt/* | tail -n 5 # Expected output: # 128904 txt/process_engineer.md # 88412 txt/maintenance_resume.md # 217316 total按长度切块。我在Claude 3.5 Sonnet中测试,单块控制在30,000到45,000中文字符,抽取稳定性高于整包上传。以下脚本按40,000字符切分。
cat > split_docs.py <<'PY' from pathlib import Path MAX=40000 out=Path("out/chunks") out.mkdir(parents=True, exist_ok=True) for p in Path("txt").glob("*"): text=p.read_text(errors="ignore") for i in range(0,len(text),MAX): chunk=text[i:i+MAX] (out/f"{p.stem}_part{i//MAX+1:03d}.txt").write_text(chunk) print(len(list(out.glob("*.txt")))) PY python3 split_docs.py # Expected output: # 17Claude提示词模板。逐块粘贴或上传。要求JSON,不要自然语言解释。
你是招聘数据抽取器。只输出JSON数组。 从文档中抽取字段: job_title, company, location, salary_min, salary_max, skills, certificates, years_required, shift_type, source_chunk。 规则: 1. 缺失字段填null。 2. salary单位统一为CNY/月。 3. skills输出数组。 4. 不要编造。 5. source_chunk使用我给你的文件名。 文档文件名:cnc_operator_jd_part001.txt 文档内容: <粘贴文本> # Expected output: # [ # { # "job_title": "CNC操作工", # "company": "宁波某精密制造有限公司", # "location": "奉化", # "salary_min": 8000, # "salary_max": 11000, # "skills": ["法兰克系统", "三菱系统", "游标卡尺"], # "certificates": null, # "years_required": 2, # "shift_type": "两班倒", # "source_chunk": "cnc_operator_jd_part001.txt" # } # ]本地校验JSON。把Claude输出保存为out/jobs.json。字段错、尾逗号、解释性文字都会被抓出来。
python3 -m json.tool out/jobs.json > /tmp/jobs.pretty.json echo $? head -n 12 /tmp/jobs.pretty.json # Expected output: # 0 # [ # { # "job_title": "CNC操作工", # "company": "宁波某精密制造有限公司"转CSV给HR或业务系统。
cat > json_to_csv.py <<'PY' import json,csv rows=json.load(open("out/jobs.json")) fields=["job_title","company","location","salary_min","salary_max","skills","certificates","years_required","shift_type","source_chunk"] with open("out/jobs.csv","w",newline="") as f: w=csv.DictWriter(f,fieldnames=fields) w.writeheader() for r in rows: r["skills"]=";".join(r["skills"] or []) w.writerow({k:r.get(k) for k in fields}) print(len(rows)) PY python3 json_to_csv.py head -n 3 out/jobs.csv # Expected output: # 30 # job_title,company,location,salary_min,salary_max,skills,certificates,years_required,shift_type,source_chunk # CNC操作工,宁波某精密制造有限公司,奉化,8000,11000,法兰克系统;三菱系统;游标卡尺,,2,两班倒,cnc_operator_jd_part001.txt
Warning:工资、证书、年限属于高风险字段。不要让模型“推测”。提示词必须写“缺失填null”。如果岗位文档来自不同企业,source_chunk必须保留,便于回溯。
如何验证已修好:抽样10条CSV,人工对照原文。通过标准:岗位名100%正确;工资上下限错误不超过1条;技能字段不得出现原文没有的技能;JSON校验退出码必须为0。在我的30份文档测试中,首次抽取28条有效,修正OCR断行后达到30条。
References:官方路线可直接使用Claude网页端或API;免费路线可用本地转换脚本加人工粘贴;付费网络工具只解决访问稳定性,不解决脏数据。商都加速器仅作为访问方案之一:https://wizzegroup.com。