![](/img/trans.png)
[英]How to read merged cell values of an excel file with openpyxl / python?
[英]openpyxl - adding new rows in excel file with merged cell existing
假設您要在表格頂部添加3行,首先必須向下移動合並的單元格,然后插入這些行; 為此,我們將使用shift(col_shift=0, row_shift=0)
方法;
Help on method shift in module openpyxl.worksheet.cell_range:
shift(col_shift=0, row_shift=0) method of openpyxl.worksheet.cell_range.CellRange instance
Shift the range according to the shift values (*col_shift*, *row_shift*).
:type col_shift: int
:param col_shift: number of columns to be moved by, can be negative
:type row_shift: int
:param row_shift: number of rows to be moved by, can be negative
:raise: :class:`ValueError` if any row or column index < 1
您要插入的行數必須與移動的行數一致; 因此,對於您的示例,您只需要:
merged_cells_range = ws.merged_cells.ranges
for merged_cell in merged_cells_range:
merged_cell.shift(0,3)
ws.insert_rows(1,3)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.