excel追加数据

原本是想通过读取已存在的文件的行然后直接添加保存,发现结果会被覆盖。

后来查找方法发现需要复制原表。

函数参数:

list:要添加的数据

filename:目标文件

sheet_index:默认表的第一个sheet

def addExcel(list,filename,sheet_index=0):
    old_sheet=xlrd.open_workbook(filename)           #读取原来的excel
    sheet_temp =old_sheet.sheet_by_index(sheet_index)#找到第一张表sheet0
    nrows = sheet_temp.nrows                         #原表
    ncols = sheet_temp.ncols                         #原表列
    new_sheet=copy(old_sheet)                        #新表复制
    sheet=new_sheet.get_sheet(sheet_index)       #新表获得第一个表sheet0
    for i in range(len(list)):              #写入
        sheet.write(nrows, i, list[i])
    os.remove(filename)                              #删除原表
    new_sheet.save(filename)                #保存新表
原文地址:https://www.cnblogs.com/51python/p/10577200.html