Python基础2--EXCEL表格处理

Python基础2--EXCEL表格处理_第1张图片

# encoding: utf-8
"""
Author: 沙振宇
CreateTime: 2019-12-6
UpdateTime: 2019-12-6
Info: 读取Excel文件,可以读取xls,也可以读xlsx
"""
import xlrd
import xlsxwriter
import xlwt
from dateutil import rrule
import datetime

def read_info_excel():
    
    file_name = 'file/BringUp.xlsx'
    workbook = xlrd.open_workbook(file_name) # 打开文件
    sheet = workbook.sheet_by_index(0) # 根据sheet索引或者名称获取sheet内容 sheet索引从0开始
    print(file_name, sheet.name, "人数:",sheet.nrows) # sheet的名称,行数,列数
    print("获取第1行内容:", sheet.row_values(0))
    

    for rown in range(sheet.nrows):
        array = {'工号': '', '姓名': '', '团队': '','数量': ''}
        array['工号'] = sheet.cell_value(rown, 0)
        array['姓名'] = sheet.cell_value(rown, 1)
        array['团队'] = sheet.cell_value(rown, 2)
        array['数量'] = 0
        excel_tables.append(array)

    #for i in range(len(excel_tables)):
    for item in excel_tables:
        print("工号:", item['工号'], "姓名:", item['姓名'],"团队:", item['团队'],"数量:", item['数量'])
    return excel_tables

def read_table_excel():
    file_name = 'file/Table.xlsx'
    workbook = xlrd.open_workbook(file_name) # 打开文件
    sheet = workbook.sheet_by_index(0) # 根据sheet索引或者名称获取sheet内容 sheet索引从0开始
    print(file_name, sheet.name, "人数:",sheet.nrows) # sheet的名称,行数,列数

    for rown in range(sheet.nrows):
        #print("姓名", sheet.cell_value(rown, 1))
        for item in excel_tables:
            mem_name = item['姓名']
            #print("mem_name", mem_name, "sheet.cell_value(rown, 1)", sheet.cell_value(rown, 1))
            if (sheet.cell_value(rown, 1) == mem_name):
                item['数量'] = item['数量']+1
                    
#    for item in excel_tables:
#        print("工号:", item['工号'], "姓名:", item['姓名'],"团队:", item['团队'],"数量:", item['数量'])   

    return excel_tables


# 写(生成的Excel.xlsx)Excel文件
def write_table_excel():
    workbook = xlsxwriter.Workbook('file/Excel_main.xlsx')  # 创建一个excel文件
    worksheet = workbook.add_worksheet()  # 在文件中创建一个名为TEST的sheet,不加名字默认为sheet1
    worksheet.set_column('A:A', 20)
    worksheet.set_column('D:D', 20)
    worksheet.set_column('E:E', 20)
    worksheet.set_column('F:F', 20)
    worksheet.set_column('G:G', 20)
    worksheet.set_column('H:H', 20)
    worksheet.set_column('I:I', 20)    
    worksheet.write('A1', '工号')
    worksheet.write('B1', '姓名')
    worksheet.write('C1', '团队')
    worksheet.write('D1', '改进建议应提交数量')
    worksheet.write('E1', '改进建议已提交数量')
    worksheet.write('F1', '当前日期')
    worksheet.write('G1', '截止日期')
    worksheet.write('H1', '截止日期前每月应提交')


    untilYear  = 2020
    untilMonth = 12
    untilDay   = 31
    
    # 2020年第一天
    firstDay = datetime.datetime(untilYear,6,7)
    endDay = datetime.datetime(untilYear,untilMonth,untilDay)
    days = rrule.rrule(freq = rrule.DAILY,dtstart=firstDay,until=endDay)

    for item in excel_tables:
        print("工号:", item['工号'], "姓名:", item['姓名'],"团队:", item['团队'],"数量:", item['数量']) 
        
    cowNum = 1
    for item in excel_tables:
        mem_name  = item['姓名']
        mem_id    = item['工号']
        mem_num   = item['数量']
        mem_team  = item['团队']
        # print("SimilarList",SimilarList)
        worksheet.write(cowNum, 0, mem_id)
        worksheet.write(cowNum, 1, mem_name)
        worksheet.write(cowNum, 2, mem_team)
        worksheet.write(cowNum, 3, 20-mem_num)
        worksheet.write(cowNum, 4, mem_num)
        worksheet.write(cowNum, 5, datetime.datetime.now().strftime('%Y-%m-%d'))
        worksheet.write(cowNum, 6, "2020-12-31")
        #worksheet.write(cowNum, 7, xlwt.Formula("=((D2-E2)/((G2-F2)/30))"))      #写入C3,数值等于2(A3+B3)
        workfomat = workbook.add_format()
        workfomat.set_num_format('0.0')     #格式化数据格式为小数点后两位
        worksheet.write(cowNum, 7, (20-mem_num)/(int)(days.count()/30),workfomat)
        cowNum = cowNum + 1

    workbook.close()




if __name__ == '__main__':
    excel_tables = []  #代表list列表数据类型,列表是一种可变序列。创建方法既简单又特别
    # 字典变量
    #array = {'工号': '', '姓名': '', '团队': '','数量': ''}
    read_info_excel()
    read_table_excel()
    write_table_excel()

你可能感兴趣的:(Python基础2--EXCEL表格处理)