Using workbook.save() when typing into single excel cell

47 Views Asked by At

I am entering in a simple string to a worksheet on an existing workbook to signify that no exceptions have been found in the code for that particular business check - 'No Data to Display'

I have the below code in order to do this but for some reason, when I use the referenced save path from the config file 'config.sod_report_full_name' it doesn't save the text in the cell.

If I hard code the save path as a normal string it does work... but I can't use this way as the save path is dynamic.

file_source = config.sod_report_full_name
workbook = openpyxl.load_workbook(filename=file_source)
worksheet = workbook['Shareclass Check 2']
print("Writing - No Data to Display")
worksheet['C5'] = 'No Data to Display'
workbook.template = False
workbook.save(file_source)
workbook.close()
print("No exceptions for Shareclass 2 check")
2

There are 2 best solutions below

0
PandasBeginner On BEST ANSWER

The openpyxl was classing with my writer method causing an overwrite to occur on the spreadsheet.

Created a datafram and trimmed to one cell and pasted in using writer instead.

1
Gareth On

worksheet['C5'] is a cell object containing several properties like the column and row of the cell and other information like the formatting. To actually write to the cell you need to assign the string to the value property.

worksheet['C5'].value = 'No Data to Display'