python - openpyxl loop through two spreadsheets match primary key and find differences -
is there way in python using openpyxl loop through 2 spreadsheets , link them primary key , find differences in column. example below uses "name" primary key , creates new column differences
example
new.xlsx: bob, 1 jim, 2 steve, 3 old.xlsx: bob, 100 steve, 200 results on new.xlsx: bob, 1, 99 jim, 2, 0 steve, 3, 197
something this
wb1 = load_workbook(r'new.xlsx') wb2 = load_workbook(r'old.xlsx') ws1 = wb1.get_sheet_by_name("sheet1") ws2 = wb2.get_sheet_by_name("sheet1") rownum in range(2, ws1.max_row): ws1row = ws1.cell(row=rownum, column=1).value ws1row2 = ws1.cell(row=rownum, column=2).value rownum2 in range(2, ws2.max_row): ws2row = ws2.cell(row=rownum2, column=1).value ws2row2 = ws2.cell(row=rownum2, column=2).value print ws2row if ws2row == ws1row: ws1.cell(row=rownum, column=3).value = ws1row2 - ws2row2 wb1.save(r'new.xlsx')
there 2 possibilities:
- create third workbook write result
- when want insert new row have shift down every value of every cells bellow, , can take lot of time ...
Comments
Post a Comment