简体   繁体   English

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

[英]python openpyxl insert_cols changes merge-cells and styles

I tried to insert a column into an excel. 我试图在Excel中插入一列。

However, the style of cells has been changed 但是,单元格的样式已更改

CODE: 码:

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 https://i.stack.imgur.com/hl5QY.png

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

This code will insert 3 columns; 该代码将插入3列; It keeps the background color but not all of the borders. 它保留背景颜色,但不保留所有边框。

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

在此处输入图片说明

After some digging I wrote this code in openpyxl and xlrd/xlwt/xlutils. 经过一番挖掘后,我在openpyxl和xlrd / xlwt / xlutils中编写了这段代码。 Support both xls and xlsx. 同时支持xls和xlsx。

Before 之前 在此处输入图片说明

After 在此处输入图片说明

The key is to use copy and generate coordinate. 关键是使用copy并生成坐标。 Code is here 代码在这里

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

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