python openpyxl insert_cols 更改合并单元格和样式

python openpyxl insert_cols changes merge-cells and styles

我试图在 excel 中插入一列。

但是,单元格的样式已经改变

代码:

import openpyxl

wb = openpyxl.load_workbook('xt3.xlsx')
sheet = wb.worksheets[0]

sheet.insert_cols(0)
[enter image description here][1]wb.save("filename.xlsx")

https://i.stack.imgur.com/hl5QY.png

关于 bitbucket 的问题:https://bitbucket.org/openpyxl/openpyxl/issues/1098/bugs-insert_cols-changes-merge-cells-and

此代码将插入 3 列;它保留背景颜色但不保留所有边框。

merged_cells_range = ws.merged_cells.ranges
for merged_cell in merged_cells_range:
    merged_cell.shift(3,0)
ws.insert_cols(1,3)

经过一番挖掘,我在 openpyxl 和 xlrd/xlwt/xlutils 中编写了这段代码。 同时支持 xls 和 xlsx。

之前

之后

关键是使用copy并生成坐标。 代码是 here