私信  •  关注

ShinNShirley

ShinNShirley 最近创建的主题
ShinNShirley 最近回复了
3 年前
回复了 ShinNShirley 创建的主题 » Python和openpyxl-在新文件中“追加”一行时,如何复制样式?

为什么必须使用函数 append ?

如:

import openpyxl,os
from copy import copy

path = '1.xlsx'
wb_1 = openpyxl.load_workbook(path)
ws_1 = wb_1[wb_1.sheetnames[0]]
wb_2 = openpyxl.Workbook()
ws_2 = wb_2.active
ws_2.title = "SHEET"
ws_2.sheet_format = ws_1.sheet_format

for (row, col), source_cell  in ws_1._cells.items():
    cell = ws_2.cell(column=col, row=row)
    cell.value = source_cell.value
    cell.font = copy(source_cell.font)
    cell.fill = copy(source_cell.fill)
    cell._hyperlink = source_cell._hyperlink
    cell.comment = source_cell.comment
wb_2.save('11.xlsx')