excel

import openpyxl

#模板表的名称
fileDir='D:\\xxi\\liangxi 1\\fileDir\\'
tempFileName = fileDir+'temp.xlsx'
fileName = fileDir + 'hehe.xlsx'

#开始创建模板
def createTemplateExcel():
    wb = openpyxl.Workbook()
    sht0 = wb.active
    sht0.title= "原数据"
    sht0.merge_cells('A1:F1')
    sht0['A1']='用户账户余额'
    sht0['A2']='用户名'
    sht0['B2']='余额'

    print("输入4个用户的用户名和余额:")
    for i in range(4):
        name=input()
        num=input()
        sht0['A%d' % (i+3)]=name
        sht0['B%d' % (i+3)]=num

    wb.save(tempFileName)


#写入历史数据
def writeTemplateInfo0(sht):
    try:
       sht['C2']='预期结果'
       sht['D2']='实际结果'
       sht['E2']='执行的判断结果'
    except BaseException as er:
        print("表头的更改发生错误")
    print("输入需要充值用户的用户名和需充值的金额:")
    name=input()
    num=input()
    for i in range(4):
        x=str(sht['A%d' % (i+3)].value)
        if x == name:
           y=sht['B%d' % (i+3)].value
           sht['D%d' % (i+3)].value=y+int(num)

    wb.save(fileName)

#数据比较
def DataComparison(sht):
    for i in range(4):
        if sht['D%d' % (i+3)].value != sht['C%d' % (i+3)].value:
            sht['E%d' % (i+3)]="用户执行错误"
        else:
            sht['E%d' % (i+3)]="用户执行正常"

    wb.save(fileName)

#createTemplateExcel()
wb = openpyxl.load_workbook(fileName)
sheet0 = wb['原数据']
writeTemplateInfo0(sheet0)
wb = openpyxl.load_workbook(fileName)
sheet0 = wb['原数据']
DataComparison(sheet0)

 

posted @ 2018-04-28 14:38  枫虹  阅读(150)  评论(2编辑  收藏  举报