我正在尝试将工作簿中的一个名为default_sheet
的表复制到一个新表new_sheet
中。
我已经成功创建了一个新表,并从默认表格复制了值。如何将每个单元格的样式也复制到新表格的单元格中呢?
new_sheet = workbook.create_sheet()
new_sheet.title = sheetName
default_sheet = workbook.get_sheet_by_name('default')
new_sheet = workbook.get_sheet_by_name(sheetName)
for row in default_sheet.rows:
col_idx = float(default_sheet.get_highest_column())
starting_col = chr(65 + int(col_idx))
for row in default_sheet.rows:
for cell in row:
new_sheet[cell.get_coordinate()] = cell.value
<copy also style of each cell>
我目前正在使用openpyxl 1.8.2,但我打算切换到1.8.5。
一种解决方案是使用复制:from copy import copy, deepcopy
new_sheet._styles[cell.get_coordinate()] = copy(
default_sheet._styles[cell.get_coordinate()])