简体   繁体   中英

Moving Data From One Workbook To Another With Openpyxl

I'm trying to create a new workbook with data from an already existing workbook. The existing workbook is extremely large so I have it loaded as a read-only workbook. Because of this, I need to iterate through the rows but I can't seem to figure out how to do this AND get data into the new workbook. Along with this, the data is from column A and is only put into the new workbook if the cell in column B say "IL".

for row in existing_sheet.iter_rows(min_col=2, max_col=2):
    for cell in row:
        print("CHECKING IF IT IS IN IL")
        if "IL" in str(cell.value):
            currSheet.cell(row=counter, column=1).value = existing_sheet.cell(row=counter, column=41).value

I keep getting deprecation warnings and the program is going much slower than I think it should be. When I simply do a print statement to see the cell value, it goes through all 40,000 rows in just a few minutes. My current code takes hours, if not longer.

existing_sheet.cell(row=counter, column=41).value This is what is slowing everything down. In read-only mode every call to iter_rows() or cell() will force openpyxl to parse the worksheet again. But you will need to have a wider row to get the 41st cell row[40] .

for row in ws1.iter_rows(min_col=2, max_col=41):
    if "IL" in row[2].value:
        ws2.cell(row=row[2].row, column=1).value = row[40].value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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