随笔 - 633,  文章 - 0,  评论 - 13,  阅读 - 48万
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

1.operation_excel.py

复制代码
# _*_ coding:utf-8 _*_

__author__ = 'bobby'
__date__ = '2018/7/13 15:47'

import xlrd   #导入xlrd
from xlutils.copy import copy   #导入excel 复制函数

# data = xlrd.open_workbook('../dataconfig/merchantcontent.xls')   #打开excel文件
# tables = data.sheets()[0]   #获取excel表里的sheet表的索引值为0的表,即第一个表的内容
# print(tables.nrows)   #打印表的行数
# print(tables.cell_value(1,3))   #打印单元格里第二行第三列的内容


class OperationExcel:
    def __init__(self,file_name=None,sheet_id=None):
        if file_name:
            self.file_name = file_name
        else:
            self.file_name = '../data/exceldata/测试用例模板.xls'

        if sheet_id:
            self.sheet_id = sheet_id
        else:
            self.sheet_id = 0


        self.data = self.get_data()   #获取sheet表


    #获取sheet的内容
    def get_data(self):
        data = xlrd.open_workbook(self.file_name)  # 打开excel文件
        print("打开[%s]文件"% self.file_name)
        tables = data.sheets()[self.sheet_id]    #sheet_id从0开始
        print("遍历第%s个sheet表" % self.sheet_id)
        return tables

    #获取单元格的行数
    def get_lines(self):
        tables = self.data
        print("获取到单元格的行数为%s"% tables.nrows)
        return tables.nrows

    #获取单元格的列数
    def get_lies(self):
        tables = self.data
        print("获取到单元格的行数为%s"% tables.ncols)
        return tables.ncols

    #获取某一个单元格的内容
    def get_cell_value(self,row,col):
        print("获取【%s】行【%s】列的内容" % (row,col))
        return self.data.cell_value(row,col)

    #写入数据
    def write_value(self,row,col,value):
        """
        写入excel数据
        """
        read_data = xlrd.open_workbook(self.file_name)   #读到excel
        write_data = copy(read_data)   #复制excel
        sheet_data = write_data.get_sheet(0)   #得到excel中的sheet表中的第一个sheet表
        sheet_data.write(row,col,value)   #写入数据
        write_data.save(self.file_name)   #保存表


    #根据对应的caseid找到对应行的内容
    def get_rows_data(self,case_id):
        row_num = self.get_row_num(case_id)   #先根据case_id拿到行号
        rows_data = self.get_row_values(row_num)   #再根据行号获取该行的内容
        return rows_data

    #根据对应的caseid找到对应的行号
    def get_row_num(self,case_id):
        num = 0  #默认行号等于0
        clols_data = self.get_cols_data()   #获取某一列的内容
        for col_data in clols_data:   #循环
            if case_id in col_data:  #如果case_id等于某一列的数据,则返回该列的行数
                return num
            num = num + 1   #如果没有找到,行号自增1


    #根据行号找到该行的内容
    def get_row_values(self,row):
        tables = self.data
        row_data = tables.row_values(row)
        return row_data

    #获取某一列的内容
    def get_cols_data(self,col_id=None):   #col_id=None,将col_id弄成一个可选参数
        if col_id !=None:
            cols = self.data.col_values(col_id)   #如果col_id 不为空,则返回col_id 的内容
        else:
            cols = self.data.col_values(0)   #否则默认返回第一行的内容
        return cols

if __name__ == '__main__':
    opers = OperationExcel()   #实例化
    print(opers.get_cell_value(1,1))   #打印表的行数
复制代码

 

2.读取图片中内容

复制代码
# -*- coding:utf-8 -*-


#参考网址:https://blog.csdn.net/weixin_47754149/article/details/125651707

# pip install Pillow
# pip install pytesseract

from PIL import Image
import pytesseract
import os

from util.handle_excel.operation_excel import OperationExcel

# 将读取图片的一行代码封装为一个函数,
def read_image(name):
    test_str = pytesseract.image_to_string(Image.open(name), lang='chi_sim')
    print(test_str)
    return test_str


# 获取所有文件
def getAllFiles(fire_dir):
    filepath_list = []
    for root,folder_names,file_names in os.walk(fire_dir):
        for file_name in file_names:
            file_path = root+os.sep+file_name
            filepath_list.append(file_path)
            print(file_path)
    print(filepath_list)
    return filepath_list


#获取图片文字
def get_image_str(src):
    #先给所有文件重命名
    filepath_list = getAllFiles(src)
    filepath_list_len = len(filepath_list)
    all_file_and_str_list = []
    for i in range(0,filepath_list_len):
        one_file_and_str_list = []
        image_name =filepath_list[i]
        one_file_and_str_list.append(image_name)
        image_str = read_image(image_name)
        one_file_and_str_list.append(image_str)
        all_file_and_str_list.append(one_file_and_str_list)
        #写入excel文件
        oe = OperationExcel(file_name="tupian_shuoming.xls",sheet_id=0)
        oe.write_value(row=i+1,col=0,value=image_name)
        oe.write_value(row=i+1,col=1,value=image_str)


        # #写入txt文件
        # with open("tupian_shuoming.txt",mode="a",encoding="utf-8") as f:
        #     f.write("[编号:%s" % str(i))
        #     f.write("\n\t")
        #     f.write("路径:%s" % image_name)
        #     f.write("\n\t\t")
        #     f.write("内容:%s]" % image_str)
        #     f.write("\n")

    print(all_file_and_str_list)




if __name__ == "__main__":
    src = r"F:\图片"
    get_image_str(src)
    # read_image(image_name)
复制代码

 

posted on   大话人生  阅读(194)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示