openpxyl删除行并没有向上移动合并的单元格时出现问题。例如,在第一张图片中,我有两个合并的单元格,其值分别为Alex和Bob。当我删除第2行时,Alex被上移到单个单元格,Bob被删除,并且合并后的单元格的位置保持在同一位置,而其余数据点则向上移动。当在Python之外正常使用excel时,合并的单元格将随其余数据一起向上移动。看来openpxyl希望向上移动数据值,但保持合并单元格的位置不变。解决该问题的方法是什么?预先谢谢你!
在删除第2行之前:
ws.delete_rows(2)
当我删除第2行时,会发生以下情况:
答案 0 :(得分:1)
在删除列时,我遇到了类似的问题,与该列相交的合并单元最终保持不变。
这以更令人满意的方式起作用。
代码提供了delete_row()
函数,该函数可用于模仿Excel行为并相应地移动合并的单元格:
import openpyxl
from openpyxl.utils import range_boundaries
from openpyxl.utils.cell import _get_column_letter
from openpyxl.worksheet.cell_range import CellRange
def delete_row(target_row):
# Assuming that the workbook from the example is the first worksheet in a file called "in.xlsx"
workbook = openpyxl.load_workbook('in.xlsx')
ws = workbook.worksheets[0]
affected_cells = [] # array for storing merged cells that need to be moved
row = target_row # specify the row that we want to delete
sheet_boundary = [4,6] # specify how far to search for merged cells in the sheet in the format of [ max_col, max_row ]
## Define a range of cells that are below the deleted row
# top left corner of the range; will be A2
tl_corner = "A"+str(row)
# bottom right corner of the row; will be D6
br_corner = _get_column_letter(sheet_boundary[0]) + str(sheet_boundary[1])
target_row_range_string = tl_corner+":"+br_corner
# express all cells in the row that is to be deleted as object CellRange from openpyxl
target_range = CellRange(range_string=target_row_range_string)
# loop over all merged cells in the sheet
for merged_cell_range in ws.merged_cells.ranges:
# if merged_cell is within target_range add current merged cell to 'affected_cells'
if merged_cell_range.issubset(target_range):
print("Cell '"+str(merged_cell_range)+"' is within range of '"+str(target_range)+"'")
affected_cells.append(merged_cell_range)
# unmerge all affected cells
for cell in affected_cells:
# get a tuple of coordinates, instead of Xlsx notation
cell_range = range_boundaries(cell.coord)
print("'"+str(cell)+"' ---> '"+str(cell_range)+"'")
# unmerge the affected cell
ws.unmerge_cells(start_column = cell_range[0], start_row = cell_range[1],
end_column = cell_range[2], end_row = cell_range[3])
# perform row deletion as usual
ws.delete_rows(row)
# merged all affected cells
for cell in affected_cells:
# get a tuple of coordinates, instead of Xlsx notation
cell_range = range_boundaries(cell.coord)
# merge back the affected cell, while lifting it up by one row
ws.merge_cells(start_column = cell_range[0], start_row = cell_range[1]-1,
end_column = cell_range[2], end_row = cell_range[3]-1)
# save the edited workbook
workbook.save('out.xlsx')
# call our custom function, specifying the row you want to delete
delete_row(2)
这将找到与target_range
相交的所有合并单元格,该范围以删除行开始并以sheet_boundary
定义的范围结束,然后首先将它们合并。
仅更改完全位于target_range
中的合并单元格。
如果合并的单元格的一部分在target_range
内,则不会对该单元格执行任何操作。
然后,它会删除所需的行,并合并所有受影响的单元格,同时考虑到它们已经向上移动了一行。
答案 1 :(得分:0)
我找到了一个临时但不令人满意的修复程序。
如果我使用以下代码:
for merged_cell in ws.merged_cell.ranges:
merged_cell.shift(0,-1)
ws.delete_rows(2)
这将解决我的问题并向上移动合并的单元格。但是,此代码的一个问题是它会将文件中所有合并的单元格向上移动。如果我只想将合并的单元格移到A列中,则不确定如何缩小范围列表以仅将那些包含在a中。
例如,以下代码无效,但突出显示了我要具体完成的工作:
ws['A'].merged_cells.ranges