zoukankan      html  css  js  c++  java
  • python 修改excel

    from xlrd import open_workbook
    from xlutils.copy import copy
    import sys
    reload(sys)
    sys.setdefaultencoding('utf-8')

    def replaceexcel(file_path,replacedic):

    # 0 based (subtract 1 from excel row number)
    START_ROW = 1
    ismal_index = 0

    #formatting_info=True保存之前数据的格式
    rb = open_workbook(file_path,formatting_info=True)
    r_sheet = rb.sheet_by_index(0) # read only copy to introspect the file
    wb = copy(rb) # a writable copy (I can't read values out of this, only write to it)
    w_sheet = wb.get_sheet(0) # the sheet to write to within the writable copy


    #r_sheet.nrows为总行数
    for row_index in range(START_ROW, r_sheet.nrows):
    newcellvalue = ''
    oldcellvalue = r_sheet.cell_value(row_index,0)
    if isinstance(oldcellvalue, (str, unicode)):
    oldcellvalue = oldcellvalue.replace(' ','')
    try:
    findnum = oldcellvalue.find('_')
    findstr = oldcellvalue[:findnum]
    newcellvalue = replacedic[oldcellvalue[:findnum]]+oldcellvalue[findnum:]
    except:
    newcellvalue = oldcellvalue
    print newcellvalue,oldcellvalue
    w_sheet.write(row_index, ismal_index,newcellvalue)

    wb.save(file_path)

    file_path = "C:\Users\Administrator\Desktop\test.xls"
    dic = {'111':u"牛逼2",'222':u"大龙2"}
    replaceexcel(file_path,dic)
  • 相关阅读:
    Redis生存时间、删除策略和排序
    Redis事务
    Redis基本类型与常用命令
    script标签块的独立性与共享性
    jQuery UI--jquery-autohide解读
    创建美化的上传文件按钮
    css3彩色进度条
    便签效果
    使用before、after伪类制作三角形
    css3实现进度条的模拟
  • 原文地址:https://www.cnblogs.com/aloneblog/p/6408092.html
Copyright © 2011-2022 走看看