Openpyxl更改单元格值并复制下面的行

我想find包含value1|value2单元格,以便可以从该单元格中移除|value2 ,并将value1正好在其下方的行复制一份。

例如,如果一行包含: value0 value1 value2 values3|values33 values4然后我会插入一个新的行,下面将是value0 value1 value2 values33 values4和原始行将更改为value0 value1 value2 values3 values4

到目前为止,我已经设法find包含|的单元格 但不知道如何进一步发展。

总之,我想知道 :如何在find匹配项后编辑单元格,并在应用的更改下复制下一行,同时对当前行应用更改以使其不再包含该值。

 from openpyxl import load_workbook wb = load_workbook('file.xlsx') sheet = wb['Sheet1'] s = '|' for row in sheet.iter_rows(): for cell in row: if s in str(cell.value): print(cell.value) 

输出:

 value1|value2 value3|value4 ... 

IIUC,这是一个使用pandas的解决scheme

 import pandas as pd #Read excel file df = pd.read_excel('duplicate.xlsx') for c in df.columns: #for each column check if it contains required character dd = df[df[c].str.contains('|', regex=False)] if len(dd) > 0: #If contains iterate the rows for i, row in dd.iterrows(): #Split the cell value by character vals = row[c].split('|') #Check if the resultant list has more than one value if len(vals) > 1: #Create a new data frame for the number of resultant values dd = pd.DataFrame([row]*(len(vals))) rows = len(dd) roc = 0 #replace the values for v in vals: dd[c].iloc[roc] = v roc += 1 #append the new dataframe to the main data frame df = df.append(dd) #Finally remove the rows that contains character from the column in iteration df = df[~df[c].str.contains('|', regex=False)] 

Excelinput:

在这里输入图像说明

输出:

在这里输入图像说明