zoukankan      html  css  js  c++  java
  • python3 openpyxl基本操作

    #coding:utf-8
    
    import xlrd
    import xlwt
    # 读写2007 excel
    import openpyxl
    import sys
    
    #读取设备sn
    # def readSN(path):
    #     wb = openpyxl.load_workbook(path)
    #     sheet = wb.active
    #     dict = []
    #     for i in range(2, sheet.max_row +1):
    #         c = sheet["C" + str(i)].value;
    #         d = sheet["D" + str(i)].value;
    #
    #         dict.append(d)
    #         #dict.append(d)
    #         #print(c,d)
    #     return dict;
    #
    #     pass;
    # print(readSN("./sim/1.xlsx"))
    
    
    
    def read07Excel(path,path1):
        wb = openpyxl.load_workbook(path)
        sheet = wb.active
        # print(sheet.max_column)  # 获取最大列数
        # print(sheet.max_row)  # 获取最大行数
        #print(sheet['B1'].value)
        wb1 = openpyxl.load_workbook(path1)
        sheet1 = wb1.active
    
        for i in range(2,sheet.max_row):
            iccid = sheet["B"+str(i)].value;
            len_iccid = len(iccid)
            if len_iccid == 20 :
                sub_iccid = iccid[16:-1]
            elif len_iccid == 21:
                sub_iccid = iccid[17:-1]
    
            for x in range(1,sheet1.max_row):
                #print(sheet1["D"+str(x)].value)
                if sub_iccid+"N" == sheet1["D"+str(x)].value:
                    sheet["O"+str(i)].value = sheet1["C"+str(x)].value;
                    wb.save(filename=path)
    
                    print(str(sheet1["D"+str(x)].value) + " "+ str(sheet1["C"+str(x)].value) +" "+ str(iccid))
                    print()
    
                pass
    
    
    
            # 写入数据
            # s =sheet["P"+str(i)].value = "dsdaf";
            # wb.save(filename=path)
            # p = sheet["P" + str(i)].value;
            #print(sub_iccid)
    
    
        # for row in sheet.rows:
        #     for cell in row:
        #         print(cell.value, "	", end="")
        #         print(cell.column, "	", end="")
        #
        #
        #     print()
        #     sys.exit()
    
    
    # path = "./sim/2.xlsx"
    # wb = openpyxl.load_workbook(path)
    # #sheet = wb.sheetnames[0] #获取名称
    # sheet = wb.active
    # 分别返回
    #print(sheet['A1'].value)  #获取单元格A1值
    
    read07Excel("./sim/2.xlsx","./sim/1.xlsx")
    
    # wb=openpyxl.load_workbook('./sim/1.xlsx')  #打开excel文件
    # print(wb.sheetnames)  #获取工作簿所有工作表名
  • 相关阅读:
    Java核心技术
    浏览器地址栏输入url回车之后发生了些什么
    Java相关面试题总结+答案(十)
    Java相关面试题总结+答案(九)
    Java相关面试题总结+答案(八)
    Java相关面试题总结+答案(七)
    input type=‘file’方法
    禁止用户复制网页内容
    js添加背景水印
    angular双向绑定
  • 原文地址:https://www.cnblogs.com/wtcl/p/9084214.html
Copyright © 2011-2022 走看看