1. 程式人生 > >python excel-操作-xlutils 實現追加寫功能 保留原格式

python excel-操作-xlutils 實現追加寫功能 保留原格式

python模組:

    xlrd:讀取excel

    xlwt:寫入excel 缺點:excel格式無法複用 

推薦xlutils模組

    可複製原excel格式

coding:

from xlutils.copy import copy

# 1. 複製原excel模板

try:
    if not os.path.exists(goal_file) and os.path.exists(model_src) :
        shutil.copy(model_src,goal_file)         
        print '複製模版成功'    
except Exception ,e:

    print "模版檔案複製失敗"

# 2. 獲取複製excel的sheet頁

rb = xlrd.open_workbook(goal_file,formatting_info=True)   # 引數說明: formatting_info=True 保留原excel格式

rs = rb.sheet_by_index(0) 

wb = copy(rb)

ws = wb.get_sheet(0)

wb.save(goal_file)

注: xlrd模組0.8版本後不支援以xlsx為字尾名檔案