iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >怎么通过Python实现批量数据提取
  • 542
分享到

怎么通过Python实现批量数据提取

2023-07-05 14:07:14 542人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

这篇文章主要介绍“怎么通过python实现批量数据提取”,在日常操作中,相信很多人在怎么通过Python实现批量数据提取问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么通过Python实现批量数据提取”的疑

这篇文章主要介绍“怎么通过python实现批量数据提取”,在日常操作中,相信很多人在怎么通过Python实现批量数据提取问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么通过Python实现批量数据提取”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

配置需求

ImageMagick  

tesseract-OCR 

python3.7

from PIL import Image as PI

import io

import os

import pyocr.builders

from cnocr import CnOcr

import xlwt

怎么通过Python实现批量数据提取

分析上图发现票据金额为“贰拾万元整”,数据金额为大写中文,因此在导入excel之前我们需要将金额票据的数据转换成数字的格式,基于此,我们需要首先完成大写汉字和数字的转换。

def chineseNumber2Int(strNum: str):    result = 0    temp = 1  # 存放一个单位的数字如:十万    count = 0  # 判断是否有chArr    cnArr = ['壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']    chArr = ['拾', '佰', '仟', '万', '亿']    for i in range(len(strNum)):        b = True        c = strNum[i]        for j in range(len(cnArr)):            if c == cnArr[j]:                if count != 0:                    result += temp                    count = 0                temp = j + 1                b = False                break        if b:            for j in range(len(chArr)):                if c == chArr[j]:                    if j == 0:                        temp *= 10                    elif j == 1:                        temp *= 100                    elif j == 2:                        temp *= 1000                    elif j == 3:                        temp *= 10000                    elif j == 4:                        temp *= 100000000                count += 1        if i == len(strNum) - 1:            result += temp    return result

通过上述代码即可实现大写字母与数字的转换,例如输入“贰拾万元整”即可导出“200000”,再将其转换成数字后即可极大地简化表格的操作,也可以在完成表格操作的同时有利于数据归档。

接下来,我们需要分析发票的内部内容,分析下图可知,我们需要获取以下几个数据内容:“出票日期”、“汇票到账日期”、“票据号码”、“收款人”、“票据金额”、“出票人”,可以通过画图软件获取精准定位。

怎么通过Python实现批量数据提取

如图,小黑点即鼠标所在地,画图软件左下角即他的坐标。

怎么通过Python实现批量数据提取

提取出票日期

def text1(new_img):    #提取出票日期    left = 80    top = 143    right = 162    bottom = 162    image_text1 = new_img.crop((left, top, right, bottom))    #展示图片    #image_text1.show()    txt1 = tool.image_to_string(image_text1)    print(txt1)    return str(txt1)

提取金额

def text2(new_img):    #提取金额    left = 224    top = 355    right = 585    bottom = 380    image_text2 = new_img.crop((left, top, right, bottom))    #展示图片    #image_text2.show()    image_text2.save("img/tmp.png")    temp = ocr.ocr("img/tmp.png")    temp="".join(temp[0])    txt2=chineseNumber2Int(temp)    print(txt2)    return txt2

提取出票人

def text3(new_img):    #提取出票人    left = 177    top = 207    right = 506    bottom = 231    image_text3 = new_img.crop((left, top, right, bottom))    #展示图片    #image_text3.show()    image_text3.save("img/tmp.png")    temp = ocr.ocr("img/tmp.png")    txt3="".join(temp[0])    print(txt3)    return txt3

提取付款行

def text4(new_img):    #提取付款行    left = 177    top = 274    right = 492    bottom = 311    image_text4 = new_img.crop((left, top, right, bottom))    #展示图片    #image_text4.show()    image_text4.save("img/tmp.png")    temp = ocr.ocr("img/tmp.png")    txt4="".join(temp[0])    print(txt4)    return txt4

提取汇票到账日期

def text5(new_img):    #提取汇票到日期    left = 92    top = 166    right = 176    bottom = 184    image_text5 = new_img.crop((left, top, right, bottom))    #展示图片    #image_text5.show()    txt5 = tool.image_to_string(image_text5)    print(txt5)    return txt5

提取票据单据

def text6(new_img):    #提取票据号码    left = 598    top = 166    right = 870    bottom = 182    image_text6 = new_img.crop((left, top, right, bottom))    #展示图片    #image_text6.show()    txt6 = tool.image_to_string(image_text6)    print(txt6)    return txt6

在将数据全部提取完成之后,即进入设置环节,我们需要首先将所有账单文件进行提取,获取他们的文件名和路径。

ocr=CnOcr()tool = pyocr.get_available_tools()[0]filePath='img'img_name=[]for i,j,name in os.walk(filePath):    img_name=name

在获取完整后,即可进行数据导入Excel的操作。

count=1book = xlwt.Workbook(encoding='utf-8',style_compression=0)sheet = book.add_sheet('test',cell_overwrite_ok=True)for i in img_name:    img_url = filePath+"/"+i    with open(img_url, 'rb') as f:        a = f.read()    new_img = PI.open(io.BytesIO(a))    ## 写入csv    col = ('年份','出票日期','金额','出票人','付款行全称','汇票到日期','备注')    for j in range(0,7):        sheet.write(0,j,col[j])    book.save('1.csv')    shijian=text1(new_img)    sheet.write(count,0,shijian[0:4])    sheet.write(count,1,shijian[5:])    sheet.write(count,2,text2(new_img))    sheet.write(count,3,text3(new_img))    sheet.write(count,4,text4(new_img))    sheet.write(count,5,text5(new_img))    sheet.write(count,6,text6(new_img))    count = count + 1

至此,完整流程结束。

附上源码全部

from  wand.image import  Imagefrom PIL import Image as PIimport pyocrimport ioimport reimport osimport shutilimport pyocr.buildersfrom cnocr import CnOcrimport requestsimport xlrdimport xlwtfrom openpyxl import load_workbook def chineseNumber2Int(strNum: str):    result = 0    temp = 1  # 存放一个单位的数字如:十万    count = 0  # 判断是否有chArr    cnArr = ['壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']    chArr = ['拾', '佰', '仟', '万', '亿']    for i in range(len(strNum)):        b = True        c = strNum[i]        for j in range(len(cnArr)):            if c == cnArr[j]:                if count != 0:                    result += temp                    count = 0                temp = j + 1                b = False                break        if b:            for j in range(len(chArr)):                if c == chArr[j]:                    if j == 0:                        temp *= 10                    elif j == 1:                        temp *= 100                    elif j == 2:                        temp *= 1000                    elif j == 3:                        temp *= 10000                    elif j == 4:                        temp *= 100000000                count += 1        if i == len(strNum) - 1:            result += temp    return result  def text1(new_img):    #提取出票日期     left = 80    top = 143    right = 162    bottom = 162    image_text1 = new_img.crop((left, top, right, bottom))    #展示图片    #image_text1.show()    txt1 = tool.image_to_string(image_text1)     print(txt1)    return str(txt1)def text2(new_img):    #提取金额     left = 224    top = 355    right = 585    bottom = 380    image_text2 = new_img.crop((left, top, right, bottom))    #展示图片    #image_text2.show()    image_text2.save("img/tmp.png")     temp = ocr.ocr("img/tmp.png")     temp="".join(temp[0])    txt2=chineseNumber2Int(temp)    print(txt2)     return txt2 def text3(new_img):    #提取出票人     left = 177    top = 207    right = 506    bottom = 231    image_text3 = new_img.crop((left, top, right, bottom))    #展示图片    #image_text3.show()    image_text3.save("img/tmp.png")     temp = ocr.ocr("img/tmp.png")    txt3="".join(temp[0])     print(txt3)    return txt3Def text4(new_img):    #提取付款行     left = 177    top = 274    right = 492    bottom = 311    image_text4 = new_img.crop((left, top, right, bottom))    #展示图片    #image_text4.show()    image_text4.save("img/tmp.png")     temp = ocr.ocr("img/tmp.png")    txt4="".join(temp[0])     print(txt4)    return txt4def text5(new_img):    #提取汇票到日期     left = 92    top = 166    right = 176    bottom = 184    image_text5 = new_img.crop((left, top, right, bottom))    #展示图片    #image_text5.show()    txt5 = tool.image_to_string(image_text5)     print(txt5)    return txt5def text6(new_img):    #提取票据号码     left = 598    top = 166    right = 870    bottom = 182    image_text6 = new_img.crop((left, top, right, bottom))    #展示图片    #image_text6.show()    txt6 = tool.image_to_string(image_text6)     print(txt6)    return txt6   ocr=CnOcr() tool = pyocr.get_available_tools()[0] filePath='img'img_name=[]for i,j,name in os.walk(filePath):    img_name=namecount=1 book = xlwt.Workbook(encoding='utf-8',style_compression=0)sheet = book.add_sheet('test',cell_overwrite_ok=True) for i in img_name:    img_url = filePath+"/"+i    with open(img_url, 'rb') as f:        a = f.read()    new_img = PI.open(io.BytesIO(a))    ## 写入csv    col = ('年份','出票日期','金额','出票人','付款行全称','汇票到日期','备注')    for j in range(0,7):        sheet.write(0,j,col[j])    book.save('1.csv')    shijian=text1(new_img)    sheet.write(count,0,shijian[0:4])    sheet.write(count,1,shijian[5:])    sheet.write(count,2,text2(new_img))    sheet.write(count,3,text3(new_img))    sheet.write(count,4,text4(new_img))    sheet.write(count,5,text5(new_img))    sheet.write(count,6,text6(new_img))    count = count + 1

到此,关于“怎么通过Python实现批量数据提取”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!

--结束END--

本文标题: 怎么通过Python实现批量数据提取

本文链接: https://www.lsjlt.com/news/352321.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 怎么通过Python实现批量数据提取
    这篇文章主要介绍“怎么通过Python实现批量数据提取”,在日常操作中,相信很多人在怎么通过Python实现批量数据提取问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么通过Python实现批量数据提取”的疑...
    99+
    2023-07-05
  • python通过opencv实现批量剪切图片
    上一篇文章中,我们介绍了python实现图片处理和特征提取详解,这里我们再来看看Python通过OpenCV实现批量剪切图片,具体如下。 做图像处理需要大批量的修改图片尺寸来做训练样本,为此本程序借助ope...
    99+
    2022-06-04
    批量 图片 python
  • 怎么用Python读取Excel数据实现批量生成合同
    这篇文章主要介绍“怎么用Python读取Excel数据实现批量生成合同”,在日常操作中,相信很多人在怎么用Python读取Excel数据实现批量生成合同问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么用Py...
    99+
    2023-06-30
  • Python读取Excel数据实现批量生成PPT
    目录背景需求准备PPT数据PPT模板实战导入相关模块读取电影数据读取PPT模板插入数据背景 大家好,我是J哥。 我们常常面临着大量的重复性工作,通过人工方式处理往往耗时耗力易出错。而...
    99+
    2022-11-11
  • Python读取Excel数据实现批量生成合同
    目录一、背景二、准备三、实战1.安装相关库2.读取合同数据3.批量合同生成大家好,我是J哥。 在我们的工作中,面临着大量的重复性工作,通过人工方式处理往往耗时耗力易出错。而Pytho...
    99+
    2022-11-11
  • 怎么通过shell脚本批量操作mysql数据库
    这篇文章主要讲解了“怎么通过shell脚本批量操作mysql数据库”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么通过shell脚本批量操作mysql数据库”吧!创建建表语句  =====...
    99+
    2023-06-05
  • 使用Python怎么批量获取基金数据
    本篇文章给大家分享的是有关使用Python怎么批量获取基金数据,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。python可以做什么Python是一种编程语言,内置了许多有效的工...
    99+
    2023-06-07
  • SpringBoot Redis怎么批量存取数据
    这篇文章主要介绍“SpringBoot Redis怎么批量存取数据”,在日常操作中,相信很多人在SpringBoot Redis怎么批量存取数据问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”SpringBoo...
    99+
    2023-06-20
  • Python怎么通过地址获取变量
    本文小编为大家详细介绍“Python怎么通过地址获取变量”,内容详细,步骤清晰,细节处理妥当,希望这篇“Python怎么通过地址获取变量”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。通过地址获取变量类似于C/C+...
    99+
    2023-06-30
  • 怎么用Python进行栅格数据的分区统计和批量提取
    小编给大家分享一下怎么用Python进行栅格数据的分区统计和批量提取,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!有时候我们会有这样的想法,就是针对某个区域的栅格数据,要提取它的平均值或者其他统计指标,比如在一个省内提取多...
    99+
    2023-06-15
  • python pdfplumber库批量提取pdf表格数据转换为excel
    目录需求一、实现效果图二、pdfplumber 库三、代码实现1、导入相关包2、读取 pdf , 并获取 pdf 的页数3、提取单个 pdf 文件,保存成 excel4、提取文件夹下...
    99+
    2022-11-11
  • 通过Python的pdfplumber库提取pdf中表格数据
    文章目录 前言一、pdfplumber库是什么?二、安装pdfplumber库三、查看pdfplumber库版本四、提取pdf中表格数据1.引入库2.定义pdf文件路径3.打开pdf文件4.获...
    99+
    2023-09-05
    python pdf 开发语言
  • Golang使用协程实现批量获取数据
    目录使用channel使用WaitGroup应用到实践服务端经常需要返回一个列表,里面包含很多用户数据,常规做法当然是遍历然后读缓存。 使用Go语言后,可以并发获取,极大提升效率。 ...
    99+
    2023-02-07
    Golang协程批量获取数据 Golang批量获取数据 Golang 获取数据
  • python数据怎么批量写入数据库
    在Python中,可以使用循环和SQL语句将数据批量写入数据库。以下是一个示例,展示了如何使用Python的`sqlite3`模块将...
    99+
    2023-10-11
    python 数据库
  • Python数据获取实现图片数据提取
    目录一、利用exifread提取图片的EXIF信息二、循环遍历图片信息比如我随便从手机上传一张图片到我的电脑里,通过python可以获取这张照片的所有信息。如果是数码相机拍摄的照片,...
    99+
    2022-11-12
  • golang mongodb批量写入数据怎么实现
    在Golang中,可以使用MongoDB的BulkWrite方法来实现批量写入数据。 首先,你需要安装MongoDB的Go驱动包,可...
    99+
    2023-10-27
    golang mongodb
  • 用Python进行栅格数据的分区统计和批量提取
    有时候我们会有这样的想法,就是针对某个区域的栅格数据,要提取它的平均值或者其他统计指标,比如在一个省内提取多年的降雨数据,最后分区域地计算一些统计值,或者从多个栅格数据中提取某个区域的数值形成一个序列。为了方便,画一...
    99+
    2022-06-02
    Python 栅格数据 python Python 栅格分区统计 Python 栅格批量提取
  • oracle存储过程怎么批量添加数据
    要批量添加数据,可以使用Oracle的FORALL语句结合BULK COLLECT功能。以下是一个示例的存储过程,用于批量添加数据:...
    99+
    2023-08-21
    oracle
  • Python实现批量导入1000条xlsx数据
    遇到的问题: 用户批量导入数据1000条,导入不成功的问题,提示查询不到商品资料。这个场景需要依靠批量的数据,每次测试的时候需要手动生成批量的数据,然后再导入操作,费时费劲。所以写了...
    99+
    2023-02-16
    Python 批量导入xlsx Python 批量导入
  • SQLServer数据库怎么批量获取所有表和数据
    本篇内容主要讲解“SQLServer数据库怎么批量获取所有表和数据”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“SQLServer数据库怎么批量获取所有表和数据...
    99+
    2022-10-18
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作