简体   繁体   English

如何在 Python 中使用 openpyxl 迭代每一行并同时插入行?

[英]How do I iterate over every row and insert rows at the same time with openpyxl in Python?

I am trying to use Openpyxl and iter_row() to iterate through every row of an excel spreadsheet and take the strings in each cell, split them by commas into values, add rows for each comma it found, then insert the values that were split out into their own rows/cells.我正在尝试使用 Openpyxl 和 iter_row() 遍历 Excel 电子表格的每一行并获取每个单元格中的字符串,用逗号将它们拆分为值,为找到的每个逗号添加行,然后插入被拆分的值进入他们自己的行/单元格。

Sample before script has run data:脚本运行数据之前的示例:
(152, 126), (14, 13) (152, 126), (14, 13)
(159, 144), (24, 43) (159, 144), (24, 43)

Cell A1 for example contains a string of 152, 126, cell B2 has the 14... Cell A2 has the 159... etc.例如,单元格 A1 包含一串 152、126,单元格 B2 具有 14...单元格 A2 具有 159...等。

After script has run data:脚本运行数据后:
(152), (14) (152)、(14)
(126), (13) (126)、(13)
(159), (24) (159)、(24)
(144), (43) (144)、(43)

Note: I am just doing an initial pass on the first column of data to add rows, then after that I'm going to go and split the data out for each cell into the appropriate rows/columns.注意:我只是对第一列数据进行初始传递以添加行,然后我将去将每个单元格的数据拆分为适当的行/列。 I am failing at adding the rows so far, I can't get it to continue adding them from rows after the first set... Any ideas?到目前为止,我未能添加行,我无法让它在第一组之后继续从行添加它们......有什么想法吗? Thanks!谢谢!

My initial failing code that is trying to insert the rows:我最初尝试插入行的失败代码:

from openpyxl import load_workbook
workbook = load_workbook(filename="test.xlsx")

sheet = workbook.active

def insert_comma_rows():
    for idx, row in enumerate(sheet.iter_rows()):
        for cell in row:
            if cell.column == 1:
                curr_val = str(cell.value)
                comma_count = curr_val.count(",")
                if(comma_count > 0):
                    sheet.insert_rows(idx=(idx+2), amount=comma_count)

insert_comma_rows()

for row in sheet.iter_rows(values_only=True):
    print(row)

On the first loop sheet.insert_rows(idx=(idx+2), amount=comma_count) will add a blank row, with the index (idx+2) as 2 and the amount of rows curr_val.count(",") as 1.在第一个循环sheet.insert_rows(idx=(idx+2), amount=comma_count)将添加一个空行,索引(idx+2)为 2,行数curr_val.count(",")为1.

It looks like instead of adding blank rows you want to add data.看起来您想要添加数据而不是添加空白行。 Consider using sheet.append() with passes in an array of values for each column eg sheet.append(['col1', 'col2', col3')])考虑使用sheet.append()并传入每列的值数组,例如sheet.append(['col1', 'col2', col3')])

As an example:举个例子:

from openpyxl import load_workbook
workbook = load_workbook(filename="test.xlsx")
    
sheet = workbook.active
    
def insert_comma_rows():
    for idx, row in enumerate(sheet.iter_rows()):
        col1 = row[0].value.split(",")
        col2 = row[1].value.split(",")

        sheet.append([col1[0].strip(), col2[0].strip()]) # strip removes whitespaces.
        sheet.append([col1[1].strip(), col2[1].strip()])
        
insert_comma_rows()

for row in sheet.iter_rows(values_only=True):
    print(row)

Output:输出:

('152, 126', '14, 13')
('159, 144', '24, 43')
('152', '14')
('126', '13')
('159', '24')
('144', '43')

It's not really relevant for the question, but I would suggest separating code into methods to fetch the data, transform it and then save it to a different file .它与问题无关,但我建议将代码分离为获取数据、转换数据然后将其保存到不同文件的方法

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

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