Python根据关联列处理两个execl并写入新execl

前几天就业管理群里需要整理已经签约的学生的相关信息,这次该我整理了,本以为很简单,就是下载execl,删除不需要的列而已呗,但是是我想象的太美好。汇总的execl需要的很多信息 签约execl里面没有,还要去学生execl里面找。一个一个吗??不可能的,几百号人呢。
我本来用的execl的LOOKUP函数,但是找完之后发现还要对学生根据班级排序,然后增加序号列等等等。那次的汇总execl的过程太令我痛苦了,碰巧看到了python处理execl就来试一下。
import xlrd
import xlwt
import time

workbook = xlwt.Workbook(encoding='utf-8')
write_sheet = workbook.add_sheet('Sheet1')
style = xlwt.XFStyle()  # 初始化样式
font = xlwt.Font()  # 为样式创建字体
font.name = '宋体'
font.height = 20 * 12  # 字体大小,11为字号,20为衡量单位
font.bold = False  # 黑体
font.underline = False  # 下划线
font.italic = False  # 斜体字
style.font = font  # 设定样式

write_sheet.write(0, 0, "列头", style) #汇总execl里面的第一行列头  我的execl有13列 你们自己定啊
write_sheet.write(0, 1, "列头", style)
write_sheet.write(0, 2, "列头", style)
write_sheet.write(0, 3, "列头", style)
write_sheet.write(0, 4, "列头", style)
write_sheet.write(0, 5, "列头", style)
write_sheet.write(0, 6, "列头", style)
write_sheet.write(0, 7, "列头", style)
write_sheet.write(0, 8, "列头", style)
write_sheet.write(0, 9, "列头", style)
write_sheet.write(0, 10, "列头", style)
write_sheet.write(0, 11, "列头", style)
write_sheet.write(0, 12, "列头", style)

data_excel1 = xlrd.open_workbook("D:\签约信息.xls")
data_sheet1 = data_excel1.sheet_by_index(0)
row1 = data_sheet1.nrows  # 总行数
col1 = data_sheet1.ncols  # 总列数

data_excel2 = xlrd.open_workbook("D:\学生信息.xlsx")
data_sheet2 = data_excel2.sheet_by_index(0)
row2 = data_sheet2.nrows  # 总行数
col2 = data_sheet2.ncols  # 总列数

#
colValue1 = data_sheet1.col_values(2)  # 签约信息中学号所在的列的list
colValue2 = data_sheet2.col_values(0)  # 学生信息中学号所在的列的list  

global x
x = 1   # 汇总execl 从第二行开始写入 第一行是上面设置好的列头
for i in range(3, len(colValue2)):   # 3 是因为 学生execl 的前三行 不需要 视情况而定
   for j in range(1, len(colValue1)):  # 1 是因为 签约execl 的前一行 不需要 视情况而定
      if colValue2[i] == colValue1[j]:  # 如果两者的学号相同 提取出两个execl里面需要的列信息到汇总execl
         write_sheet.write(x, 0, x, style)  # 第一列  序号的值 就是x
         write_sheet.write(x, 1, "XX学院", style) # 第二列 我这边是我们院系名字 视情况而定
         rowValue = data_sheet2.row_values(i)  # 根据i值 获取 学生execl 中的那一行的值
         for colNum in range(0, 4):  # range(0,4)是 学生execl 需要的数据
            write_sheet.write(x, colNum + 2, rowValue[colNum], style) 
         rowValue = data_sheet1.row_values(j)
         y = 6
         for colNum in [47, 13, 19, 21, 22, 23, 61]:  # [47, 13, 19, 21, 22, 23, 61]是 签约execl 需要的数据
            write_sheet.write(x, y, rowValue[colNum], style)
            y = y + 1
         x = x + 1

workbook.save('D:\'+time.strftime("%Y-%m-%d")+'签约学生汇总.xls') # 写入到文件中
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值