python 读取excel数据插入到另外一个excel

时间:2021-11-15 01:19:12
#-*-coding:utf-8-*-
import xlrd
import xlwt


def excel_copy(dir_from, dir_to, sheet_name):
    '''从一个excel写入到另外一个excel'''

    wb = xlrd.open_workbook(dir_from)
    # 选择sheet页
    sheet1 = wb.sheet_by_index(0)
    # 打印每个sheet页的行数
    # print("sheet1行数:%d" % sheet1.nrows)
    # 创建写入文件
    workbook = xlwt.Workbook(encoding="ascii")
    # 创建写入sheet页
    worksheet = workbook.add_sheet(sheet_name)
    # 写入excel
    for i in range(0, sheet1.nrows):
        values_row1 = sheet1.row_values(i)
        print(values_row1)
        for s in range(len(values_row1)):
            worksheet.write(i, s, values_row1[s])
    workbook.save(dir_to)