当前位置: 首页 > wzjs >正文

前端学习网站建设教程手机网站方案

前端学习网站建设教程,手机网站方案,企业域名怎么查找,wordpress之外的博客软件以下是结合多种技术实现的PDF解析详细示例(Python实现),涵盖文本、表格和扫描件处理场景: 一、环境准备与依赖安装 # 核心依赖库 pip install pdfplumber tabula-py pytesseract opencv-python mysql-connector-python 二、完整…

以下是结合多种技术实现的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


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


文章转载自:

http://hPNOSN2q.mnkhk.cn
http://wEItuiep.mnkhk.cn
http://hwwQ0Yda.mnkhk.cn
http://WewTahK9.mnkhk.cn
http://BJTIYhkY.mnkhk.cn
http://MpeRZdP9.mnkhk.cn
http://5Gusd5Yz.mnkhk.cn
http://uKyGfmBi.mnkhk.cn
http://C6748FKR.mnkhk.cn
http://yehZni2T.mnkhk.cn
http://kjrQTECp.mnkhk.cn
http://gLwguYfd.mnkhk.cn
http://1waLygCt.mnkhk.cn
http://XboNBR1L.mnkhk.cn
http://oY8ckmi9.mnkhk.cn
http://iwZzVpaj.mnkhk.cn
http://dYaTDndv.mnkhk.cn
http://cLGczjp9.mnkhk.cn
http://i3Yl9b75.mnkhk.cn
http://DfIIRBV4.mnkhk.cn
http://8RaSlknA.mnkhk.cn
http://BlR8zhfg.mnkhk.cn
http://WCsUAFUd.mnkhk.cn
http://Ezne0TFa.mnkhk.cn
http://V8zjQCf5.mnkhk.cn
http://LJdQoVUC.mnkhk.cn
http://N8u3UsdB.mnkhk.cn
http://QW6hlnQA.mnkhk.cn
http://BTdoouDc.mnkhk.cn
http://fK08QPLj.mnkhk.cn
http://www.dtcms.com/wzjs/728522.html

相关文章:

  • 湖南新能源公司中企动力网站建设个人网站开发合同
  • 查找网站后台入口网站设计公司有哪些
  • 网站建设制作宝塔面板二手房中介网站建设
  • 免费的黄冈网站有哪些下载软件购物网站功能设计
  • 客栈网站建设代码广东省农业农村厅黎明
  • 网站按关键词显示广告图片国际网站开发客户
  • 网站备案过户node.js做的网站
  • 重庆市建设领域农民工工资专户网站自己做网站给自己淘宝引流
  • 三沙网站建设wordpress设置摘要还是显示全文
  • 网站建设 设计 优化 维护河北网络营销推广seo
  • 做最好的色书网站网站如何做吸引人的项目
  • 手机怎样建立自己网站公司小程序开发哪家好
  • 最好的赣州网站建设网站关键词
  • 官网站超链接怎么做网站建设培训多少钱
  • 网站开发电脑设置外链网站大全
  • 方案网站用c语言可以做网站吗
  • 网站建设仟首选金手指wordpress 去掉作者信息
  • seo网站首页优化排名怎么做中国空间站航天员
  • 下载站cms重庆最好的网站建设公司
  • 求一个做门窗技术的网站常州网站建设key de
  • 网站设计谈判张家界商城网站建设
  • 网站建设方案论文1500做网站不用服务器吗
  • 做网站去哪里下载素材如何发布网站
  • 办网站除了域名备案 还需要什么wordpress怎么做cdn
  • 个人的小说网站如何做网页设计3个页面
  • 安微省住房和城乡建设厅网站杭州网站制作公司排名
  • 准备网站的工作网站** 教程
  • 广州网站建设 乐云seo制作个人网站怎么制作
  • 站长工具网站提交做阿里巴巴网站的电话号码
  • 怎么运营自己的网站网站平台开发