Python Application

问题背景

班长总是辛苦滴,是不是就得统计什么东西,每个人写好之后发到邮箱,一个个下载过来不说,还得一个个打开,复制黏贴,重复劳动呀,我们IT人,怎么能做这些如此简单的事,所以就想着简化点事,至少我下载过来,不用一个个打开复制黏贴,于是乎写个了python脚本。

问题描述

将一些列以数字命名的xls里面的内容,合并到一个xls中

代码

 1 import xlrd, xlwt, os, sys, re
 2 #get the catalogue
 3 filenames = os.listdir(os.getcwd())
 4 #new excel for write
 5 newFile = xlwt.Workbook()
 6 #add sheet, then u can control sheet of the excel
 7 sheetW = newFile.add_sheet("sheet")
 8 #count the num of the rows
 9 count = 0
10 for num in xrange(len(filenames)):
11     #find all .xls using regular expression
12     if re.match(r'\d*\.xls$', filenames[num]):
13         #read the excel
14         file = xlrd.open_workbook(filenames[num])
15         #get the sheet of the excel
16         sheetR = file.sheets()[0]
17         #gain the row num and column num
18         nrows = sheetR.nrows
19         ncols = sheetR.ncols
20         for i in xrange(nrows):
21             #remove the title except it is the first row
22             #and if there is no title in excel, we should read directly
23             if i == 0 and count != 0 and nrows != 1:
24                 continue
25             for j in xrange(ncols):  
26                 #write every cell into the sheet
27                 sheetW.write(count, j, sheetR.cell(i, j).value)
28             count += 1
29 newFile.save('newFile.xls')
View Code

小结

1、用xlrd,xlwt实现读写,可以试试其他的

2、考虑到了内容可能存在不同,比如有些xls里没有title

3、Last but not the least,一个个cell读,效率未免低下,而且没有判读是否有重复的元素

改进

1、针对这小程序,可以让这更“智能”(判读重复,格式化输出),更“高效”(一行行写)

2、针对这背景,可以实现一个web端的,班长添加title,学生登录,填写信息,班长直接download,是不是更强大

 

转载于:https://www.cnblogs.com/chuanlong/archive/2013/05/18/3085544.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值