首页 > 代码库 > python xlwt,xlutils 在excel里面如何插入一行数据
python xlwt,xlutils 在excel里面如何插入一行数据
就是把插入行之后值重新输出来。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import xlwt; import xlrd; from xlutils.copy import copy; #styleBoldRed = xlwt.easyxf(‘font: color-index red, bold on‘); #headerStyle = styleBoldRed; #wb = xlwt.Workbook(); #ws = wb.add_sheet(‘sheetName‘); #ws.write(0, 0, "Col1", headerStyle); #ws.write(0, 1, "Col2", headerStyle); #ws.write(0, 2, "Col3", headerStyle); #wb.save(‘fileName.xls‘); #open existed xls file oldWb = xlrd.open_workbook( "fileName.xls" , formatting_info = True ); oldWbS = oldWb.sheet_by_index( 0 ) newWb = copy(oldWb); newWs = newWb.get_sheet( 0 ); inserRowNo = 1 newWs.write(inserRowNo, 0 , "value1" ); newWs.write(inserRowNo, 1 , "value2" ); newWs.write(inserRowNo, 2 , "value3" ); for rowIndex in range (inserRowNo, oldWbS.nrows): for colIndex in range (oldWbS.ncols): newWs.write(rowIndex + 1 , colIndex, oldWbS.cell(rowIndex, colIndex).value); newWb.save( ‘fileName.xls‘ ); print "save with same name ok" ; |
python xlwt,xlutils 在excel里面如何插入一行数据
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。