1 import xlrd
 2 from xlutils.copy import copy as copyWorkbook
 3 from xlutils.filter import process, XLRDReader, XLWTWriter
 4 def copy2(wb):
 5     w = XLWTWriter()
 6     process(XLRDReader(wb, 'unknown.xls'), w)
 7     return w.style_list
 8 
 9 #获取文件中的单元格样式
10 def get_style(filepath):
11     oldWb = xlrd.open_workbook(filepath, formatting_info=True, on_demand=True)
12     s = copy2(oldWb)
13     sheet1 = oldWb.get_sheet(0)
14     style = s[sheet1.cell_xf_index(0,0)]
15     oldWb.release_resources()#关闭模板文件
16     return style
17 
18 workbook = xlrd.open_workbook('10.xls', formatting_info=True, on_demand=True)
19 newWrokbook = copyWorkbook(workbook) 
20 sheet1 = workbook.get_sheet(0)
21 new_sheet1 = newWrokbook.get_sheet(0)
22 num_cols = sheet1.ncols#ncols列数,nrows行数
23 style = get_style('10.xls')
24 for i in range(num_cols):
25     if sheet1.cell(0, i).value=='总金额':
26         new_sheet1.write(0, i, '收入', style)#按照style风格写入单元格
27 workbook.release_resources()
28 newWrokbook.save('101010.xls')#文件名不同,则另存为
29 
30 #--------------以下笔记----------------------
31 row_data = sheet0.row_values(0)     # 获得第1行的数据列表
32 col_data = sheet0.col_values(0)     # 获得第1列的数据列表
33 参考:http://www.cnblogs.com/zhaoyingjie/p/6554858.html

 

posted on 2018-04-10 16:55  math98  阅读(224)  评论(0编辑  收藏  举报