I'm trying to write a scipt that can update a column based on a transaction ID. Im using Python3, Openpyxl to read the excel file
In the above image, it would be to update the highlighted cells with the same value in column K, as they have the same transaction ID in column C. Then when it gets to C12, it updates column K with a different value as the value of C has changed...and so on and so on.
So far I have:
from openpyxl import load_workbook, Workbook
import re
wb = load_workbook(filename = 'Testing.xlsx')
ws = wb['Test']
for r in range(2, ws.max_row + 1):
column_c = ws.cell(row = r, column = 3).value
column_h = ws.cell(row = r, column = 8).value
column_i = ws.cell(row = r, column = 9).value
column_j = ws.cell(row = r, column = 10).value
previous = None
while (previous == column_c):
ws.cell(row = r, column = 11).value = column_j_formatted
if (previous != column_c):
continue
wb.save('Testing_processed.xlsx')
UPDATE
I have tried to replace the while loop with:
previous_col_c = ws.cell(row=r-1, column=3)
for row_num in range (2, ws.max_row + 1):
current_col_c = ws.cell(row=r, column=3)
current_col_j = ws.cell(row=r, column=11)
if current_col_c == previous_col_c:
ws.cell(row = r, column = 11).value = column_j_formatted
previous_col_c = current_col_c
