自动旋转并合并多张图片为PDF,支持按Excel清单重命名并提取OCR文字。
Image to PDF Merger Skill是一项面向实际任务的技能,主要用于将多张图片合并成一个 PDF 文档,适合处理会计凭证、扫描文件等;支持自动旋转调整方向;
用户发送扫描版 PDF 并要求"提取文字"、"总结";1. 收集图片/PDF;让用户一次性发送所有需要处理的。它将相关步骤、工具调用和结果整理方式集中到统一流程中,帮助使用者更快完成目标并减少重复操作。
从功能定位来看,该技能强调把分散的操作要求整理成清晰、可复用的处理流程,使用户能够围绕既定目标快速准备输入、选择执行方式并获得结构化结果。实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;涉及批量任务时,还应保存进度,避免中断后重复操作。
将多张图片合并成一个 PDF 文档,适合处理会计凭证、扫描文件等。支持自动旋转调整方向。
# Python pip3 install Pillow pypdf openpyxl # 系统工具 brew install poppler tesseract tesseract-lang
让用户一次性发送所有需要处理的 PDF,不要分开多次发。
适用于图片文件(.jpg):
import os
from pypdf import PdfWriter
from PIL import Image
# 获取所有新上传的图片
images = sorted(glob.glob(f"{inbound_dir}*.jpg"), key=os.path.getmtime, reverse=True)[:20]
merger = PdfWriter()
for img_path in images:
img = Image.open(img_path)
# 自动旋转:横版图片旋转90度
width, height = img.size
if width > height:
img = img.rotate(90, expand=True)
pdf_path = img_path.replace('.jpg', '.pdf')
img.save(pdf_path, 'PDF', resolution=100.0)
merger.append(pdf_path)
output_path = f"{output_dir}{凭证号}.pdf"
merger.write(output_path)
根据 Excel 清单自动重命名 PDF:
Excel 结构:
关键:使用字母索引法,避免列号混淆!
import openpyxl
import re
import shutil
import os
# Excel 路径
excel_path = "~/.openclaw/media/inbound/Vouching_list---*.xlsx"
# 加载 Excel(使用字母索引)
wb = openpyxl.load_workbook(excel_path)
ws = wb.active
# 需要处理的文件列表 (序号, 原始文件名)
files = [
(1, "1._1月79号---xxx.pdf"),
(2, "2_1月109号---xxx.pdf"),
# ...
]
for idx, filename in files:
# Index N -> Excel Row N+1(因为 Row 1 是表头)
excel_row = idx + 1
# 使用字母索引直接访问列
desc = ws[f'H{excel_row}'].value # H列 = 摘要
ship = ws[f'M{excel_row}'].value # M列 = 船名归属
# 从文件名提取凭证号(如 "1月79号")
date_match = re.search(r'(d+月d+号)', filename)
date_part = date_match.group(1) if date_match else ""
# 命名格式:序号_凭证号-【船名】摘要.pdf
# 例如:01_1月79号-【祥瀚8】V2501运费收入.pdf
new_name = f"{idx:02d}_{date_part}-【{ship}】{desc}.pdf"
# 复制并重命名
src = os.path.join(inbound_dir, filename)
dst = os.path.join(output_dir, new_name)
shutil.copy(src, dst)
文件名格式规范:
序号_凭证号-【船名/公司】摘要.pdf 示例: 01_1月79号-【祥瀚8】V2501运费收入.pdf 02_1月109号-【River】坞修2411001.pdf 03_1月135号-【Alice】2503航次收入.pdf 04_1月169号-【Anna】DBS融资款付船款.pdf
message(
action="send",
filePath=output_path,
message=f"{序号}. {凭证号} - 【{船名}】{摘要}",
target="用户ID"
)
ws[f'H{row}'] 而非 row[7]/)需要清理当用户发送扫描版 PDF(无文字层)时,使用 tesseract 提取文字:
# 安装依赖 brew install poppler tesseract tesseract-lang # PDF 转图片(300 DPI) pdftocairo -png -r 300 input.pdf page
# 识别中文+英文 tesseract page-01.png stdout -l chi_sim+eng > output.txt # 批量识别 for p in page-*.png; do tesseract "$p" stdout -l chi_sim+eng >> combined.txt done
import re
# 读取 OCR 结果
with open('combined.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 提取关键信息(根据文档类型调整)
patterns = {
'贷款人': r'([A-Z]+ BANK LTD.?.*?)n',
'借款人': r'([A-Z]+ SHIPPING LIMITED.*?)n',
'贷款金额': r'USD([d,]+)',
'利率': r'([d.]+)%',
'期限': r'(d+)s*years?',
}
results = {}
for key, pattern in patterns.items():
match = re.search(pattern, text, re.IGNORECASE)
if match:
results[key] = match.group(1) if match.lastindex else match.group(0)
print(results)
#!/bin/bash
# pdf-ocr.sh - 从扫描版 PDF 提取文字
PDF_PATH="$1"
OUTPUT_DIR="/tmp/pdf_ocr_$(date +%s)"
mkdir -p "$OUTPUT_DIR"
# 1. PDF 转图片
echo "Converting PDF to images..."
pdftocairo -png -r 300 "$PDF_PATH" "$OUTPUT_DIR/page"
# 2. OCR 识别
echo "Running OCR..."
> "$OUTPUT_DIR/combined.txt"
for img in "$OUTPUT_DIR"/page-*.png; do
echo "Processing: $img"
tesseract "$img" stdout -l chi_sim+eng 2>/dev/null >> "$OUTPUT_DIR/combined.txt"
done
# 3. 提取关键信息
echo "Extracting key information..."
python3 << 'EOF'
import re
with open('$OUTPUT_DIR/combined.txt', 'r', encoding='utf-8', errors='ignore') as f:
text = f.read()
patterns = {
'lender': r'([A-Z]+ BANK LTD.?.*?)n',
'borrower': r'([A-Z]+ SHIPPING LIMITED.*?)n',
'amount': r'USD([d,]+)',
'date': r'(d{1,2}s+w+s+d{4})',
}
for key, pattern in patterns.items():
match = re.search(pattern, text, re.IGNORECASE)
if match:
print(f"{key}: {match.group(1)}")
EOF
echo "Output saved to: $OUTPUT_DIR/combined.txt"
为避免图片传输不完整,添加等待机制:
import time # 等待所有图片传输完成 time.sleep(5) # 等待5秒 # 获取最新上传的图片(按修改时间排序) # 这样可以确保所有已传输的图片都被处理
如果不想用固定延时,也可以:
相关专题
热门下载
相关下载
精品课程
共6课时 | 54.7万人学习
共89课时 | 134.1万人学习
共49课时 | 82.6万人学习