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

时间:2017-11-06 21:25:45

标签: python excel openpyxl

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

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

到目前为止,我已经设法找到包含|的细胞但不知道如何进一步发展。

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

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
...

1 个答案:

答案 0 :(得分:0)

IIUC,这是使用pandas

的解决方案
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)]

Excel输入:

enter image description here

输出:

enter image description here