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)
  • 相关阅读:
    华为2016校园招聘上机笔试题
    android SQLite 使用
    handler
    fragment 给 activity 传数据
    activity 给 fragment 传递数据
    fragment (动态加载)
    fragment (静态)
    Java学习随笔之磨刀篇——环境搭建+问候世界
    Go语言设计哲学
    Ubuntu设置护眼程序
  • 原文地址:https://www.cnblogs.com/aloneblog/p/6408092.html
Copyright © 2011-2022 走看看