简体   繁体   English

openpyxl - 我无法将单元格样式复制到另一个中

[英]openpyxl - I can't copy the cell style in another one

I need to copy a cell to another one with all the styles associated, including the width dimension.我需要将一个单元格复制到另一个与所有 styles 相关联的单元格,包括宽度尺寸。

below a simple example (an instruction doesn't work):下面是一个简单的例子(指令不起作用):

import openpyxl
from copy import copy

src_wb = openpyxl.load_workbook("C:\\Users\\Admin\\Desktop\\database.xlsx")
src_ws = src_wb[src_wb.sheetnames[0]]

dst_wb = openpyxl.Workbook()
dst_ws = dst_wb.active
dst_ws.title = "TEST"

src_cell = src_ws.cell(row=1, column=1)
dst_cell = dst_ws.cell(row=1, column=1)

if src_cell.has_style:
    dst_cell._style = copy(src_cell._style) # it doesn't work..

dst_cell.value = src_cell.value

dst_wb.save("test.xlsx")
dst_wb.close()

how can I fix the issue?我该如何解决这个问题?

Try this尝试这个

if cell.has_style:
    new_cell.font = copy(cell.font)
    new_cell.border = copy(cell.border)
    new_cell.fill = copy(cell.fill)
    new_cell.number_format = copy(cell.number_format)
    new_cell.protection = copy(cell.protection)
    new_cell.alignment = copy(cell.alignment)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM