用python编辑excel表格单元格

用python编辑excel表格单元格,python,excel,Python,Excel,我正在尝试用python编辑excel表格单元格。 对于编辑,我使用以下代码: from xlrd import open_workbook from xlutils.copy import copy xl_file = r'D:\path\excel.xls' rb = open_workbook(xl_file) wb = copy(rb) sheet = wb.get_sheet(0) sheet.write(0,2,'New_Data_For_Cell') wb.save(xl_fil

我正在尝试用python编辑excel表格单元格。 对于编辑,我使用以下代码:

from xlrd import open_workbook
from xlutils.copy import copy

xl_file = r'D:\path\excel.xls'
rb = open_workbook(xl_file)
wb = copy(rb)
sheet = wb.get_sheet(0)
sheet.write(0,2,'New_Data_For_Cell')
wb.save(xl_file)
使用此代码,单元格值成功更新,但整个excel工作表背景颜色格式更改为默认值


我想用更新的单元格值保留所有颜色和格式。

保留excel工作表的格式 你应该使用

格式化信息=True

使用打开的工作簿

以下是工作代码:

from xlrd import open_workbook
from xlutils.copy import copy

xl_file = r'D:\path\excel.xls'
rb = open_workbook(xl_file, formatting_info=True)
wb = copy(rb)
sheet = wb.get_sheet(0)
sheet.write(0,2,'New_Data_For_Cell')
wb.save(xl_file)

我得到的表没有属性写入。