最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python利用PaddleOCR進行掃描版PDF的跨頁表格提取與合并

 更新時間:2025年12月18日 09:01:06   作者:Quest for Knowledge  
在處理PDF文件中的表格時,常常會遇到表格跨頁的情況,并且一些PDF文件為掃描版,這種情況下,如果要將跨頁的表格合并為一個完整的表格,手動操作不僅繁瑣且容易出錯,因此,本文將介紹如何利用PaddleOCR和Python代碼,自動化地檢測并合并這些跨頁表格

前言

在處理PDF文件中的表格時,常常會遇到表格跨頁的情況。并且一些PDF文件為掃描版。這種情況下,如果要將跨頁的表格合并為一個完整的表格,手動操作不僅繁瑣且容易出錯。因此,本文將介紹如何利用PaddleOCR和Python代碼,自動化地檢測并合并這些跨頁表格。

1.環(huán)境準備

首先,我們需要安裝以下庫:

  • pandas:數(shù)據(jù)處理
  • paddleocr:OCR識別表格結構
  • pdf2image:將PDF頁面轉(zhuǎn)換為圖像
  • beautifulsoup4:解析HTML
  • numpy:數(shù)組處理

安裝命令如下:

pip install pandas paddleocr pdf2image beautifulsoup4 numpy

引入所需的庫并設置一些警告和日志配置,以確保代碼執(zhí)行過程中不會被不必要的信息干擾:

import pandas as pd
from paddleocr import PPStructure, save_structure_res
from pdf2image import convert_from_path
from bs4 import BeautifulSoup
import warnings
import numpy as np
import logging
import os

warnings.filterwarnings("ignore")
logging.disable(logging.DEBUG)
logging.disable(logging.WARNING)

2.文件路徑與閾值設置

定義PDF文件的路徑和一些參數(shù)閾值,用于判斷表格是否跨頁:

path = 'E:/Jobcontent/data/測試/'
output_folder = "E:/table/ex/"
topthreshold = 0.2
dthreshold = 0.8

table_engine = PPStructure(show_log=True)

3.定義輔助函數(shù)

這些輔助函數(shù)用于提取PDF頁面中的表格信息,并判斷表格是否跨頁。

  • top_bottom_table_info:獲取頁面中最上方表格的列數(shù)和坐標。
  • find_bottom_table_info:獲取頁面中最下方表格的列數(shù)和坐標。
  • is_continuation:判斷表格是否跨頁。
def top_bottom_table_info(table_result):
    top_table = None
    min_y = 0
    for table in table_result:
        if table['type'] == 'table':
            bbox = table['bbox']
            current_bottom_y = bbox[1]
            if top_table is None or current_bottom_y < min_y:
                top_table = table
                min_y = current_bottom_y

    if top_table is not None:
        soup = BeautifulSoup(top_table['res']['html'], 'html.parser')
        last_row = soup.find_all('tr')[-1]
        columns = last_row.find_all('td')
        top_row_columns = len(columns)
        top_xy = top_table['bbox']
        return top_row_columns, top_xy
    else:
        return None

def find_bottom_table_info(table_result):
    bottom_table = None
    bottom_y = 0
    for table in table_result:
        if table['type'] == 'table':
            bbox = table['bbox']
            current_bottom_y = bbox[3]
            if bottom_table is None or current_bottom_y > bottom_y:
                bottom_table = table
                bottom_y = current_bottom_y

    if bottom_table is not None:
        soup = BeautifulSoup(bottom_table['res']['html'], 'html.parser')
        last_row = soup.find_all('tr')[0]
        columns = last_row.find_all('td')
        last_row_columns = len(columns)
        bottom_xy = bottom_table['bbox']
        return last_row_columns, bottom_xy
    else:
        return None

def is_continuation(top_row_columns, last_row_columns, bottom_xy, top_xy, page_height, dthreshold=0.8, topthreshold=0.2):
    if top_row_columns != last_row_columns:
        return False
    is_last_table_at_bottom = bottom_xy[3] > dthreshold * page_height
    is_first_table_at_top = top_xy[1] < topthreshold * page_height
    return is_last_table_at_bottom and is_first_table_at_top

4.處理PDF文件

讀取PDF文件并提取每頁的表格信息。對于跨頁的表格,提取其列數(shù)和坐標,并將結果合并。

  1. 獲取PDF文件列表:獲取指定路徑下所有以“.pdf”結尾的文件。
  2. 逐個處理PDF文件:對于每個PDF文件,初始化一個列表來存儲跨頁表格信息。
  3. 將PDF頁面轉(zhuǎn)換為圖像:將PDF文件的每一頁轉(zhuǎn)換為圖像,并逐頁處理。
  4. 提取表格信息:使用table_engine函數(shù)從每個頁面圖像中提取表格信息,并保存結構化結果。
  5. 檢測跨頁表格:檢查當前頁的最后一行和下一頁的第一行的列數(shù)是否相同,如果相同,則記錄跨頁表格的信息。
  6. 合并跨頁表格:對于檢測到的跨頁表格,讀取跨頁的兩部分表格,合并后保存為CSV文件。
  7. 完成提?。涸谔幚硗晁蠵DF文件后,打印“表格提取完成”的消息。
pdf_files = [f for f in os.listdir(path) if f.endswith('.pdf')]
print(pdf_files)

for pdf_file in pdf_files:
    cross_page_tables = []
    pdf_path = os.path.join(path, pdf_file)
    images = convert_from_path(pdf_path, dpi=200)
    print('正在提取表格,請耐心等待...')
    for page_number, image in enumerate(images):
        table_result = table_engine(np.array(image))
        save_structure_res(table_result, output_folder, f'{page_number+1}')

        _, page_height = image.size
        last_row_columns, bottom_xy = find_bottom_table_info(table_result)
        if page_number+1 < len(images):
            table_result_end = table_engine(np.array(images[page_number+1]))
            top_row_columns, top_xy = top_bottom_table_info(table_result_end)

            if is_continuation(top_row_columns, last_row_columns, bottom_xy, top_xy, page_height):
                cross_page_tables.append((bottom_xy, top_xy, page_number+1, page_number+2))
                print(f"{pdf_file} 的表格在第 {page_number+1} 頁和第 {page_number+2} 頁之間跨頁,并且最后一行和下一頁的第一行列數(shù)相同")

    if cross_page_tables:
        for (bottom_xy, top_xy, start_page, end_page) in cross_page_tables:
            output_folder_s = output_folder + f'{start_page}/'
            output_folder_t = output_folder + f'{end_page}/'
            file_s = f'{bottom_xy}'+'_0'+'.xlsx'
            file_t = f'{top_xy}'+'_0'+'.xlsx'
            s_path = os.path.join(output_folder_s, file_s)
            e_path = os.path.join(output_folder_t, file_t)
            table_result_start =pd.read_excel(s_path, header=None)
            table_result_end = pd.read_excel(e_path, header=None)
            merged_table = pd.concat([table_result_start, table_result_end], ignore_index=True)
            output_path = os.path.join(output_folder, f'{pdf_file}_merged_{start_page}_{end_page}.csv')
            merged_table.to_csv(output_path, index=False)

print('表格提取完成')

5.總結

通過上述代碼,可以實現(xiàn)對掃描版PDF文件中跨頁表格的檢測與合并,并將結果保存為CSV文件。該方法對提升PDF表格處理的自動化程度和效率具有重要意義。

到此這篇關于Python利用PaddleOCR進行掃描版PDF的跨頁表格提取與合并的文章就介紹到這了,更多相關Python PaddleOCR PDF跨頁表格提取與合并內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 使用Python檢測文章抄襲及去重算法原理解析

    使用Python檢測文章抄襲及去重算法原理解析

    文章去重(或叫網(wǎng)頁去重)是根據(jù)文章(或網(wǎng)頁)的文字內(nèi)容來判斷多個文章之間是否重復。這篇文章主要介紹了用Python寫了個檢測文章抄襲,詳談去重算法原理,需要的朋友可以參考下
    2019-06-06
  • 使用Python設置Excel頁眉和頁腳的操作代碼

    使用Python設置Excel頁眉和頁腳的操作代碼

    在實際的商務報表和數(shù)據(jù)文檔處理中,頁眉和頁腳是提升文檔專業(yè)性和可讀性的重要元素,通過合理設置頁眉頁腳,可以展示公司名稱、文檔標題、頁碼、日期等關鍵信息,本文將介紹如何使用 Python 程序化地設置 Excel 工作表的頁眉和頁腳主題,實現(xiàn)自動化文檔格式化
    2026-05-05
  • 最新評論

    饶平县| 柳州市| 理塘县| 清水河县| 宾阳县| 泸水县| 宾阳县| 黎城县| 梁平县| 铁力市| 崇左市| 中超| 岚皋县| 广安市| 阳高县| 九寨沟县| 珠海市| 浦北县| 千阳县| 屏东县| 汽车| 临漳县| 呼伦贝尔市| 梅河口市| 宜宾市| 水城县| 西林县| 宁津县| 汝州市| 盐池县| 威远县| 吴江市| 平顺县| 彭山县| 金山区| 东乌珠穆沁旗| 青铜峡市| 乐东| 阳山县| 磴口县| 哈巴河县|