使用xlutils库带格式复制一段xls文件 修改部分内容 并将其输出到新文件

import xlrd
import xlwt
from xlutils.copy import copy
import os
def xiugai(old_path,new_path,old_name,save_name,old_name1,new_name1,new_name2):
    workbook = xlrd.open_workbook('\\'.join((old_path,old_name)),
                                  formatting_info=1)  # 打开工作簿
    wb = copy(workbook)
    worksheet = workbook.sheet_by_index(0)
    for i in range(0, worksheet.nrows):
        for j in range(0, worksheet.ncols):
            if worksheet.cell_value(i, j) == old_name1:

                font = xlwt.Font() 
                font.name = 'Times New Roman'
                font.height = 220
                style = xlwt.XFStyle()
                style.font = font

                wb.get_sheet(0).write(i, j, new_name1,style)
                wb.get_sheet(0).write(i, j + 1, new_name2,style)
    wb.save('\\'.join((new_path, save_name)))

def main():
    xiugai(old_path,new_path,old_name,save_name,old_name1,new_name1,new_name2)


你可能感兴趣的:(python)