美文网首页
python向已存在excel文件写入数据

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

作者: MiracleJQ | 来源:发表于2018-06-26 16:12 被阅读0次

    使用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";

    相关文章

      网友评论

          本文标题:python向已存在excel文件写入数据

          本文链接:https://www.haomeiwen.com/subject/dmdmyftx.html