Loop through and compare spreadsheet cells in Python
Question: Please excuse the crude code and I'm sure there are better ways to accomplish
this but I am new to programming. Basically I have an excel file with 2
sheets, sheet 1 is populated in column A, sheet 2 is populated in A, B, and C.
I want to run through all of the cells in sheet 1 column A searching for a
match in sheet 2 column A and copy the info from B and C to sheet 1 if found.
The code below kind of works, it copies some data and populates it but it
doesn't really match up correctly and it seems to skip a lot of cells if they
are the same value as the previous cell. Any help would be greatly
appreciated.

    
    
    import openpyxl
    
    wb = openpyxl.load_workbook('spreadsheet.xlsx')
    
    sheet1 = wb.get_sheet_by_name('Sheet1')
    sheet2 = wb.get_sheet_by_name('Sheet2')
    
    for row in sheet1['A1':'A200']:
        for cell in row:
            obj1 = cell.value
            for row2 in sheet2['A1':'A2000']:
                for cell2 in row2:
                    obj2 = cell2.value
                    if obj1 == obj2:
                        row = str(cell2.row)
                        site = 'B' + row
                        tic = 'C' + row
                        sheet1[site] = sheet2[site].value
                        sheet1[tic] = sheet2[tic].value
    
    wb.save('spreadsheet2.xlsx')
    


Answer: Your question is a little unclear but if I understand you correctly this
should help:

    
    
    import openpyxl
    
    wb = openpyxl.load_workbook('spreadsheet.xlsx')
    
    sheet1 = wb.get_sheet_by_name('Sheet1')
    sheet2 = wb.get_sheet_by_name('Sheet2')
    
    
    for i in range(1, 201):
        if sheet1.cell(row = i, column = 1).value == sheet2.cell(row = i, column = 1).value:
            sheet1.cell(row = i, column = 2).value = sheet2.cell(row = i, column = 2).value
            sheet1.cell(row = i, column = 3).value = sheet2.cell(row = i, column = 3).value
    
    
    wb.save('spreadsheet2.xlsx')
    

I was able to clean up the code by the using the `.cell()` method. If this
isn't what you need just comment and tell me what exactly you are trying to
do. Hope this helps!

