Python Excel(xlrd,xlwt) - 如何从一个单元格复制样式并将其放在另一个单元格上

Gre*_*reg 7 python excel xlrd xlwt xlutils

具体来说,我正在尝试打开现有的工作簿,并向其中写入一些数据.

但是,每当我写入数据时,它都会消除这些单元格上的边框.

所以我想知道是否有办法在写入之前复制该单元格的样式然后重新应用它.

我想我可能会使用这段代码走上正轨吗?

from xlrd import open_workbook
from xlwt import easyxf
from xlutils.copy import copy
from xlutils.styles import Styles

rb=open_workbook('source.xls',formatting_info=True)
styles = Styles(rb)
rs=rb.sheet_by_index(0)
wb=copy(rb)
ws=wb.get_sheet(0)

for i,cell in enumerate(rs.col(2)):
    if not i:
      continue
    cell_style = styles[rs.cell(i,2)]
    ws.write(i,2,cell.value,cell_style)

wb.save('output.xls')
Run Code Online (Sandbox Code Playgroud)

但是我收到了这个错误:

AttributeError: NamedStyle instance has no attribute 'font'
Run Code Online (Sandbox Code Playgroud)

Cit*_*ito 3

这里的问题是 xlrd.NamedStyle 与 xlwt.XFStyle 有很大不同。

这个问题似乎是Preserving styles using python's xlrd,xlwt, and xlutils.copy的重复