当前位置:   article > 正文

使用gradio创建一个提取pdf、excel中表格数据的demo_gradio pdf

gradio pdf

使用Gradio创建一个提取pdf、excel中表格数据的demo

在线体验地址 (https://swanhub.co/patch/TabularScan/demo)

大家可以在上面的链接中试用,需求不大也不用自己弄代码了。
后续大家如果有一些代码或功能想快速部署、提供服务,不管是 AI 项目或是 web 项目,也可以直接托管在 swanhub开源社区 上,方便快捷,而且免费

最近需要对pdf、excel文件中的表格进行提取,用于一些分析,所以使用python完成了一个小工具,可以处理上传的pdf、excel文件,将其中所有表格提取出后存入数组输出:

import gradio as gr
import pdfplumber
import os
import openpyxl


def process_pdf(file):
    file_extension = os.path.splitext(file.orig_name)[-1]

    tables = []

    if file_extension == ".pdf":
        with pdfplumber.open(file.orig_name) as pdf:
            for page in pdf.pages:
                table = page.extract_tables()
                tables.append(table)
    elif file_extension == '.xlsx':
        excel = openpyxl.load_workbook(file.orig_name)
        for name in excel.sheetnames:
            sheet = excel[name]

            max_row = sheet.max_row
            max_column = sheet.max_column

            for row in sheet.iter_rows(values_only=True):
                row_data = []
                for cell_value in row:
                    row_data.append(cell_value)  # 将单元格值添加到当前行的数据列表
                tables.append(row_data)  # 将当前行的数据列表添加到主数组

    return tables


iface = gr.Interface(
    fn=process_pdf,
    inputs=gr.inputs.File(type="file"),
    outputs="text",
    title="上传 PDF/Excel 文件",
    description="提取上传文件中的所有表格,并以数组形式输出",
)

iface.launch()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

其中使用到了几个库:

  • 提取 pdf 使用到的:pdfplumber
  • 提取 excel 使用到的:openpyxl

两个库的使用方法不难,文档可以直接在github上找到

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/94359
推荐阅读
相关标签
  

闽ICP备14008679号