PDF解析示例代码学习

以下是结合多种技术实现的PDF解析详细示例(Python实现),涵盖文本、表格和扫描件处理场景:


一、环境准备与依赖安装

# 核心依赖库 pip install pdfplumber tabula-py pytesseract opencv-python mysql-connector-python


二、完整解析流程示例

复制代码
import pdfplumber import tabula import pytesseract import cv2 import re import mysql.connector from mysql.connector import Error from PIL import Image import hashlib # ========== 1. 通用PDF解析 ========== ‌:ml-citation{ref="6,7" data="citationList"} def parse_pdf(pdf_path): result = {"text": [], "tables": [], "images": []} # 文本提取(含坐标信息) with pdfplumber.open(pdf_path) as pdf: for page in pdf.pages: # 基础文本提取 text = page.extract_text(x_tolerance=1, y_tolerance=1) if text: result["text"].append({ "page": page.page_number, "content": text_clean(text), "bbox": page.bbox }) # 表格识别与提取 ‌:ml-citation{ref="3" data="citationList"} tables = page.find_tables() if tables: result["tables"].extend([ {"table_data": table.extract(), "position": table.bbox} for table in tables ]) # 扫描件处理模块 ‌:ml-citation{ref="6" data="citationList"} if not result["text"]: result.update(process_scanned_pdf(pdf_path)) return result # ========== 2. 扫描件OCR处理 ========== ‌:ml-citation{ref="6" data="citationList"} def process_scanned_pdf(pdf_path, dpi=300): ocr_results = [] images = convert_pdf_to_images(pdf_path, dpi) # 使用pdf2image库转换 for img in images: processed_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) text = pytesseract.image_to_string(processed_img, lang='chi_sim+eng') ocr_results.append(text_clean(text)) return {"ocr_text": ocr_results} # ========== 3. 数据标准化处理 ========== ‌:ml-citation{ref="7" data="citationList"} def text_clean(raw_text): # 多级清洗流程 text = re.sub(r'\s{2,}', ' ', raw_text) # 压缩空白字符 text = re.sub(r'[\x00-\x1F\x7F-\x9F]', '', text) # 删除控制字符 return text.strip() # ========== 4. 结构化字段提取 ========== ‌:ml-citation{ref="4,7" data="citationList"} def extract_structured_data(texts): patterns = { "date": r'\d{4}年\d{1,2}月\d{1,2}日', "amount": r'金额:(\d+\.\d{2})元', "parties": r'甲方:(.*?)\n乙方:(.*?)\n' } structured_data = {} for pattern_name, regex in patterns.items(): matches = [] for text in texts: matches.extend(re.findall(regex, text)) structured_data[pattern_name] = matches return structured_data # ========== 5. 数据库存储 ========== ‌:ml-citation{ref="8" data="citationList"} def save_to_mysql(data, pdf_hash): try: conn = mysql.connector.connect( host='localhost', database='pdf_archive', user='root', password='' ) cursor = conn.cursor() insert_query = """ INSERT INTO documents (file_hash, raw_text, structured_data) VALUES (%s, %s, %s) """ cursor.execute(insert_query, ( pdf_hash, "\n".join(data['text']), json.dumps(data['structured']) )) conn.commit() except Error as e: print(f"数据库错误: {e}") finally: if conn.is_connected(): cursor.close() conn.close() # ========== 6. 主执行流程 ========== if __name__ == "__main__": pdf_file = "sample_contract.pdf" # 生成文件指纹 ‌:ml-citation{ref="6" data="citationList"} with open(pdf_file, "rb") as f: file_hash = hashlib.sha256(f.read()).hexdigest() # 执行解析 parsed_data = parse_pdf(pdf_file) structured = extract_structured_data(parsed_data['text']) parsed_data['structured'] = structured # 数据持久化 save_to_mysql(parsed_data, file_hash) 

三、关键处理策略说明

  1. 混合解析机制

    • 优先尝试直接文本提取(可编辑PDF)‌6
    • 自动降级到OCR处理(扫描件)‌6
    • 保留原始坐标信息用于后期验证‌4
  2. 表格识别增强

    使用PDF页面的物理布局检测表格边界(page.find_tables()),配合tabula进行精确提取‌3

  3. 多语言OCR支持

    通过lang='chi_sim+eng'参数实现中英文混合识别,需提前安装Tesseract中文训练包‌6

  4. 数据验证机制

    • 通过file_hash字段实现重复文件过滤‌6
    • 同时保存原始文本和结构化数据用于数据追溯‌8

四、数据库表结构设计

CREATE TABLE `documents` ( `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, `file_hash` CHAR(64) NOT NULL UNIQUE, `raw_text` LONGTEXT, `structured_data` JSON, `ocr_flag` TINYINT(1) DEFAULT 0, `create_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


五、异常处理建议

  1. 编码兼容性

    添加数据库连接参数charset='utf8mb4'支持生僻字存储‌8

  2. 大文件分块处理

    使用生成器逐页处理超过50页的PDF文档:

    def batch_process(pdf_path, batch_size=10): with pdfplumber.open(pdf_path) as pdf: total_pages = len(pdf.pages) for i in range(0, total_pages, batch_size): yield pdf.pages[i:i+batch_size]

  3. 容错机制

    在解析循环中添加异常捕获:

    try: text = page.extract_text() except PDFSyntaxError as e: logging.warning(f"Page {page_num} parse failed: {str(e)}") continue


该示例实现了从基础文本提取到复杂表格识别的完整流程,并包含扫描件处理方案,实际应用中需根据具体文档结构调整正则表达式模式和表格识别参数‌

相关推荐
西岸行者7 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
百事牛科技7 天前
保护文档安全:PDF限制功能详解与实操
windows·pdf
悠哉悠哉愿意7 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
别催小唐敲代码7 天前
嵌入式学习路线
学习
毛小茛7 天前
计算机系统概论——校验码
学习
babe小鑫7 天前
大专经济信息管理专业学习数据分析的必要性
学习·数据挖掘·数据分析
winfreedoms7 天前
ROS2知识大白话
笔记·学习·ros2
在这habit之下7 天前
Linux Virtual Server(LVS)学习总结
linux·学习·lvs
我想我不够好。7 天前
2026.2.25监控学习
学习
im_AMBER7 天前
Leetcode 127 删除有序数组中的重复项 | 删除有序数组中的重复项 II
数据结构·学习·算法·leetcode