怎么用python修改excel表格內容 python怎么保存excel文件
在工作中經(jīng)常需要修改excel文件并保存(例如添加行、添加列),如果只使用xlwt和xlrd并不能達成目的,需要用到xlutils。下面以我實(shí)際寫(xiě)的代碼為例
備注:代碼格式本來(lái)好好的,不知道為什么發(fā)表后就亂了,沒(méi)有縮進(jìn)
1.python是2.7
2.調試環(huán)境是:eclipse+pydev
*.用到的python插件:xlrd、xlutils
*.實(shí)際代碼如下:
#encoding:utf-*
import xlrd
import shutil
from xlutils.copy import copy
from xlutils.margins import null_cell_types
def ana_file(aExcelFile):
#global gOutput_log_str
#download all zip files
r_xls=xlrd.open_workbook(aExcelFile)
table = r_xls.sheet_by_index(0)
nrows=table.nrows
ncols = table.ncols
#write the column key
w_xls = copy(r_xls)
sheet_write = w_xls.get_sheet(0)
sheet_write.write(0, gBug_id_col, "Bug ID")
sheet_write.write(0, gBug_newflag_col, "New Bug")
sheet_write.write(0, gBug_comment_col, "Comments")
sheet_write.write(0, gBug_findclose_col, "Bug Close")
w_xls.save(aExcelFile)
shutil.move(aExcelFile, os.path.join(os.path.split(aExcelFile)[0],os.path.splitext(os.path.split(aExcelFile)[1])[0]+".xls"))