Python3使用xlrd库读取execel表格(xls格式)



https://www.jb51.net/article/205141.htm

在python中利用xlrd/xlwt对excel进行关键字查找并生成新表 - 宫爆鸡丁丁 - 博客园

python-使用xlrd读取一行或一列的数据_冷雨寒风的博客-CSDN博客_xlrd 读取一行

python xlrd获取第一行值 - 问答 - Python中文网

python3.8.5自带xlrd吗_使用python3+xlrd解析Excel的实例_weixin_39710041的博客-CSDN博客

python3 excel数据处理_Python3使用xlrd、xlwt处理Excel方法数据_艾苛尔的博客-CSDN博客

python中使用xlrd、xlwt操作excel表格详解_谷曰十鑫的博客-CSDN博客

# -*- coding: UTF-8 -*-
import re
import os
import time
import xlrd

readPWD = os.popen('pwd').readlines()
replace_path = readPWD[0].replace('\n','/')#'/media/vdb1/amf_auto_check/'

log_text = []

def myprint(*args):
    log_text.append(' '.join(args)+'\n')
    print(log_text[-1], end="")

def getgitlog(excelPath):
    print("================================================================================================================================================")
    print(excelPath)
    sheets_data = xlrd.open_workbook(excelPath)
    try:
        sheet_command_table = sheets_data.sheet_by_name('命令')
        # print("=============")
        # print (sheet_command_table.nrows)  #行数
        # print (sheet_command_table.ncols)  #列数
        print("----------------------------------------------")
        # print(sheet_command_table.cell(0,0).value)
        # print(sheet_command_table.cell(0,1).value)
        # print(sheet_command_table.cell(0,2).value)
        # print(sheet_command_table.cell(0,0).value)
        # print(sheet_command_table.cell(1,0).value)
        # print(sheet_command_table.cell(2,0).value)
        # print("=============")
        for idx in range(0,sheet_command_table.nrows):
            #print(sheet_command_table.row_slice(idx))
            #print(sheet_command_table.row(idx))
            #print(idx)
            #row_data = sheet_command_table.row(idx)
            row_data = sheet_command_table.row_values(rowx=idx, start_colx=0, end_colx=None)
            #print(row_data)
            #print("=============")
            for i in range(0,len(row_data)):
                #print(i)
                object_str = str(row_data[i])
                #if object_str.find('String') > 0:
                ###if 'String' in object_str:
                ###if 'IPV4' in object_str or 'IPV6' in object_str:
                ###if 'LongDate' in object_str:
                if 'Password' in object_str:
                    print("-------------")
                    print(row_data)
                    print("-------------")
                    print("字段: %s" % str(row_data[i - 5]))
                    print("类型: %s" % str(row_data[i]))
                    print("范围: %s" % str(row_data[i + 2]))
                    print("正则: %s" %str(row_data[i + 6]))
                    print("-------------")
    except KeyError as e:
        print("=============1")
        print(e)
        print("=============1")
    except BaseException as e:
        print("=============2")
        print(e)
        print("=============2")

    excelPath = excelPath.replace(replace_path, '')
    p_file = f"{excelPath}       "
    project = excelPath.split('/')[0]
    r = os.popen("cd {} \n git log --pretty=format:'<%an>%Creset(%cr)' --abbrev-commit -1 {}".format(project, excelPath.replace(project + '/', '')))
    for line in r.readlines():
        # deal_list.append(str(line).split(' ')[-1].replace('\n',''))
        myprint(p_file+line)
    print("----------------------------------------------")


def print_target(excelPath):
    getgitlog(excelPath)
    # excels = xlrd.open_workbook(excelPath)
    # sheetNames = excels.sheet_names()  # 获取目录下所有sheet的名字,sheetNames为list类型
    # has_spec_cmd(sheetNames, excels, excelPath)
    # defalut_value_check(sheetNames, excels, excelPath)

if __name__ == '__main__':
    cur_path = os.path.abspath('.')
    # myprint(cur_path)
    try:
        os.listdir(os.path.join(cur_path, r'am/'))
    except Exception as e:
        myprint('请确保find.py和am在同级目录!', e)
        quit()
    #search_path = [ r'rm/oam_model', r'am/oam_model', r'comm/oam_model', r'em/oam_model', r'Location/oam_model', r'mt/oam_model']
    search_path = [ r'am/oam_model', r'comm/oam_model']
    myprint('默认搜索目录:\n')
    for s_dir in search_path:
        myprint(os.path.join(cur_path, s_dir).replace(replace_path, ''))
    data_paths = set()
    for s_dir in search_path:
        myprint("\n\n===============\n=%s=\n==============\n\n" % s_dir)
        for root, dirs, files in os.walk(os.path.join(cur_path, s_dir)):
            for f in files:
                if str(f).split('.')[-1] == 'xls' or str(f).split('.')[-1] == 'xlsm':
                    excelPath = os.path.join(root, f)
                    try:
                        print_target(excelPath)
                    except Exception as e:
                        myprint('\n', e, os.path.join(root, f))
    time_tuple = time.localtime(time.time())
    log_file = './log/%04d%02d%02d.txt' % (time_tuple[0], time_tuple[1], time_tuple[2])
    with open(log_file, 'w') as f:
        f.writelines(log_text)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值