本文介绍两种操作Excel时修改数据后原有样式不变的方法。
1. xlrd + xlutils.copy
这种方式主要参考了这篇文章 [Preserving styles using python’s xlrd,xlwt, and xlutils.copy]
import xlrd
from xlutils.copy import copy
def set_out_cell(out_sheet, col, row, value):
"""
核心函数, 保留样式地修改单元格的值
:param out_sheet: 工作表
:param col: 列索引
:param row: 行索引
:param value: 修改的值
:return:
"""
def _get_out_cell(_out_sheet, col_index, row_index):
_row = _out_sheet._Worksheet__rows.get(row_index)
if not _row:
return None
cell = _row._Row__cells.get(col_index)
return cell
previous_cell = _get_out_cell(out_sheet, col, row)
out_sheet.write(row, col, value)
if previous_cell:
new_cell = _get_out_cell(out_sheet, col, row)
if new_cell:
new_cell.xf_idx = previous_cell.xf_idx
def set_cell_with_style():
file = r'C:\Users\Administrator\Desktop\test.xls'
workbook = xlrd.open_workbook(file, formatting_info=True)
workbook_copy = copy(workbook)
worksheet = workbook_copy.get_sheet(0) # 获取第一个工作表
set_out_cell(worksheet, 0, 0, 'helloworld') # 修改数据
workbook_copy.save('output.xls')
注意: 使用此方式时一定要确定文件的格式为xls,不能是xlsx。
否则会报错(NotImplementedError: formatting_info=True not yet implemented)
报错的原因是xlrd在目前的版本(v1.2.0)不支持读取xlsx文件时带有formatting_info参数了。
另外还需要注意的是,这个方法几乎保留了所有的格式,但是仍然会丢失单元格注释和其他工作表中的单元格引用
2. win32com
第二种方式采用win32com库直接调用windows api,这会使得它的功能十分强大,但是效率低下耗时长,对于大文件/大量数据修改的场景不太友好,请权衡使用。
def set_cell_with_style_xlsx():
file = r'C:\Users\Administrator\Desktop\test.xlsx'
app = client.Dispatch("Excel.Application")
app.Visible = False
book = app.Workbooks.Open(file)
sheet = book.Worksheets(1) # 打开第一个表
# sheet = book.Worksheets('Sheet1') # 打开名字为Sheet1的表
info = sheet.UsedRange
# 修改数据
sheet.Cells(1, 1).Value = 'Hello World'
# book.save() # 直接保存
book.SaveAs('output.xlsx') # 另存为 - 默认保存位置:C:\Users\Administrator\Documents
book.Close(True) # 关闭文件
app.Quit() # 退出excel
注意:使用此方式时,不要打开待修改的Excel文件,不然可能会出现不可预知的问题(pywintypes.com_error)
此方式支持xlsx,全格式保留,但是效率低。