简体   繁体   English

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

[英]Openpyxl changing a cell value and copying the row below

I would like to find cells which contain value1|value2 so that I can remove |value2 from that cell and make a copy of the row where value1 was right below it. 我想找到包含value1|value2单元格,以便可以从该单元格中除去|value2 ,并复制value1就在其下的行的副本。

For example if a row contains: value0 value1 value2 values3|values33 values4 I would then insert a new row below which would be value0 value1 value2 values33 values4 and the original row would be changed to value0 value1 value2 values3 values4 . 例如,如果某行包含: value0 value1 value2 values3|values33 values4然后,我将在其下面插入新行,其中将是value0 value1 value2 values33 values4 ,而原始行将更改为value0 value1 value2 values3 values4

So far I have managed to find the cells which contain | 到目前为止,我设法找到了包含|的单元格。 but do not know how to progress further. 但不知道如何进一步发展。

In conclusion I'd like to know : How can I edit the cell upon finding the match and copy a row below it with the applied change while also applying a change to the current row so that it no longer contains that value. 总之,我想知道 :如何找到匹配项后如何编辑单元格,并使用已应用的更改在它下面复制一行,同时还对当前行应用更改,以使其不再包含该值。

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)

Output: 输出:

value1|value2
value3|value4
...

IIUC, here is a solution using pandas 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 input: Excel输入:

在此处输入图片说明

Output: 输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM