繁体   English   中英

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

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

我想找到包含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
...

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输入:

在此处输入图片说明

输出:

在此处输入图片说明

暂无
暂无

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

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