私信  •  关注

eNc

eNc 最近创建的主题
eNc 最近回复了

我在这里提出的解决方案是一种适应 from this post openpyxl documentation

我的输入xlsx如下所示:

enter image description here

代码如下:

import openpyxl

# file 1:
wb_1 = openpyxl.load_workbook(in_path, read_only=True)
ws_1 = wb_1[wb_1.sheetnames[0]]

# file 2 (it's a new file):
wb_2 = openpyxl.Workbook()
ws_2 = wb_2.active

for r in ws_1.rows:
    for c in r:
        ws_2.cell(row=c.row, column=c.column).value = c.value
        ws_2.cell(row=c.row, column=c.column).fill = c.fill
        ws_2.cell(row=c.row, column=c.column).font = c.font
        ws_2.cell(row=c.row, column=c.column).number_format = c.number_format
        ws_2.cell(row=c.row, column=c.column).border = c.border

wb_2.save(out_path)

这是我的输出文档:

enter image description here

基本上,我的内部循环是代码的副本:

    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)

所以他的部分功劳归于@CharlieClark (link is here again)

如果你想让别人回答你的问题,至少试着使用他们在评论中提供的信息。尤其是如果您是python或某个特定包的新手。这不是一个编程服务,你至少应该试着找出一个解决方案,然后再声称你的帖子不是重复的或者问题还没有得到回答。