I am trying to create Python code that reads data from a certain cell in an Excel file and returns a specified value based on a dictionary key.
I am using a for loop to iterate over each row, checking if the key exists within the cell.value located in column 'D' and want it to then assign the value to a column 'H' and row x where x is the current row.
logging.debug(wb.sheetnames)
ws = wb['Sheet1']
for cell in ws['D']:
for k, v in dictKey.items():
if k in cell.value:
logging.debug(v)
ws['H1'] = v
I have shown H1 as a static example of what I am trying to achieve but want it to be H2, H3, H4 for each row being interated.
I thought perhaps I could create a simple:
count = 0
count += 1
at the start of the for loop and then concatenate the value using:
ws['H' + str(count)] = v
But i thought there could be a more elegant solution using an inbuilt function in openpyxl?
I could not find a clean solution after a quick search and wondering if the above would work/be good code?
Thanks
I believe you're looking for ws.cell(row, column).value
which let's you get and set based on index (index starting 1 cause it's openpyxl).
https://openpyxl.readthedocs.io/en/stable/api/openpyxl.cell.cell.html?highlight=cell.value
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.