python向已存在excel文件写入数据

使用xlutils先拷贝再写入。(保存样式)

import xlwt;

import xlrd;

#import xlutils;

from xlutils.copy import copy;


styleBoldRed =xlwt.easyxf('font: color-index red, bold on');

headerStyle =styleBoldRed;

wb =xlwt.Workbook();

ws =wb.add_sheet(gConst['xls']['sheetName']);

ws.write(0, 0, "Header",        headerStyle);

ws.write(0, 1, "CatalogNumber", headerStyle);

ws.write(0, 2, "PartNumber",    headerStyle);

wb.save(gConst['xls']['fileName']);


#open existed xls file

#newWb = xlutils.copy(gConst['xls']['fileName']);

#newWb = copy(gConst['xls']['fileName']);

oldWb =xlrd.open_workbook(gConst['xls']['fileName'], formatting_info=True);

printoldWb; #

newWb =copy(oldWb);

printnewWb; #

newWs =newWb.get_sheet(0);

newWs.write(1, 0, "value1");

newWs.write(1, 1, "value2");

newWs.write(1, 2, "value3");

print"write new values ok";

newWb.save(gConst['xls']['fileName']);

print"save with same name ok";

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容