Skip to content

Instantly share code, notes, and snippets.

@bhaskar253
Created May 31, 2020 18:50
Show Gist options
  • Select an option

  • Save bhaskar253/9e7934dae53a723d8e03dc488c516634 to your computer and use it in GitHub Desktop.

Select an option

Save bhaskar253/9e7934dae53a723d8e03dc488c516634 to your computer and use it in GitHub Desktop.
Updating the existing cell of the excel file using Python (# Delete it once its use is over)
import openpyxl
excel_file = openpyxl.load_workbook('records.xlsx')
#print(excel_file.sheetnames)
tc_sheet = excel_file['Testcases']
#currently_active_sheet = excel_file.active
cell_obj = tc_sheet.cell(row=1, column=1)
#print(f'{cell_obj.value}')
#print(f'{tc_sheet["A1"].value}')
#print(f'rows = {tc_sheet.max_row} & cols = {tc_sheet.max_column}')
trows = int(tc_sheet.max_row)
tcols = int(tc_sheet.max_column)
lst = [{"id": "t_02.tc", "res": "FAIL"}, {"id": "t_01.tc", "res": "BLOCK"}]
lst.sort(key=lambda i: int(i['id'].split('.tc')[0].split('t_')[1]))
for d in lst:
k = d['id'].split('.tc')[0]
r = d['res']
for i in range(2, trows + 1):
if tc_sheet.cell(row=i, column=1).value == k:
tc_sheet.cell(row=i, column=3).value = r
#for i in range(2, trows + 1):
# tc_sheet.cell(row=i, column=3).value = 'PASS'
excel_file.save('records.xlsx')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment