Zou*_*ino 4 python excel python-2.7 python-3.x openpyxl
所以,我试图在 excelsheet 中添加 6 行。我使用 openpyxl.worksheet.worksheet.Worksheet.insert_rows(ws,idx=0,amount=6) 来帮助我完成任务。此行与普通的 excel 文件完美配合。
但是,当涉及到包含合并单元格的 excel 文件时。该程序将无法像我附加的图像一样正常工作。
有人可以给我一些有关如何解决问题的想法。我用完了所有的想法,需要一些灵感。
非常感谢回答我问题的人!!
假设您想在表格顶部添加 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
Run Code Online (Sandbox Code Playgroud)
您要插入的行数必须与您移动的行数一致;因此,对于您的示例,您只需要:
merged_cells_range = ws.merged_cells.ranges
for merged_cell in merged_cells_range:
merged_cell.shift(0,3)
ws.insert_rows(1,3)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2651 次 |
| 最近记录: |