zoukankan      html  css  js  c++  java
  • 使用python读写excel

      项目中要在excel要跨工作簿根据一列数据获取另一列的数据,而excel本身的函数vlookup一直不太好用,只能用程序进行处理了,最近刚接触了python,灵机一动使用Python进行处理,先将json格式处理成csv格式,再保存为excel,由于工作日报中要根据之前的json数据进行统计,数据行较大,人工进行工作量较大,然后使用python根据excel的内容进行处理。

    import xlrd
    import xlwt
    from xlutils.copy import copy
    import openpyxl
    
    sheetName='20170716'
    dailyName='工作日报'
    
    dailyData=xlrd.open_workbook(dailyName+'.xlsx')
    dailySheets=dailyData.sheets()
    dailySheetNames=dailyData.sheet_names()
    index=dailySheetNames.index(sheetName)
    #print(index)#获取sheet索引
    # for sheet in dailySheetNames:
    #     print(sheet)
    dailyTable=dailySheets[index]
    dailyRows=dailyTable.nrows
    dailyCols=dailyTable.ncols
    
    #print(dailyRows)
    #日报文档
    data = xlrd.open_workbook(sheetName+'.xlsx')
    table = data.sheets()[0]
    nrows = table.nrows #行数
    ncols = table.ncols #列数
    
    wb=openpyxl.load_workbook(dailyName+'.xlsx')
    sheet=wb.get_sheet_by_name(sheetName)
    #newWs=newWb.get_sheet(index)
    #oldWb.save('test123.xlsx')
    for i in range(0,dailyRows):
        for j in range(0, nrows):
            dailyRowValues = dailyTable.row_values(i)
            rowValues = table.row_values(j)  # 某一行数据
            #print('1'+rowValues[0])
            #print('2'+dailyRowValues[4])
            #print(dailyRowValues[4]==rowValues[0])
            if dailyRowValues[4]==rowValues[0]:
                print(i)
            # for item in rowValues:
                sheet['G'+str(i+1)]=rowValues[1]
                sheet['H'+str(i+1)]=rowValues[2]
                if rowValues[2].find('无匹配保单数据')>-1:
                    sheet['I' + str(i + 1)] = '是'
                else:
                    if rowValues[2].find('无符合查询条件的保单数据')>-1:
                        sheet['I' + str(i + 1)] = '是'
    
            #     print(item)
    wb.save(dailyName+'.xlsx')
    

      

  • 相关阅读:
    Python学习笔记009_构造与析构
    Python学习笔记008_类_对象_继承_组合_类相关的BIF
    Python学习笔记007_图形用户界面[EasyGui][Tkinter]
    Python学习笔记006_异常_else_with
    Python学习笔记005_文件_OS_模块_pickle
    Python学习笔记004_字典_集合
    小甲鱼:Python学习笔记003_函数
    小甲鱼:Python学习笔记002_数组_元组_字符串
    Java数据库连接泄漏应对办法-基于Weblogic服务器
    java单点登录
  • 原文地址:https://www.cnblogs.com/dzlishen/p/7192257.html
Copyright © 2011-2022 走看看