Manu
Manu

Reputation: 1

Replace excel cell value if cell contains text and don't replace text If it is empty

I have a requirement that replaces excel cell value(string/text) with "X" and doesn't replace it if it is empty.

Upvotes: 0

Views: 344

Answers (1)

ytung-dev
ytung-dev

Reputation: 902

Given that no use case or details are provided, see if this is what you want.

import openpyxl

wb = openpyxl.load_workbook('input.xlsx')
ws = wb.worksheets[0]

for row in ws.iter_rows():
    for cell in row:
        if cell.value != None:
            cell.value = 'X'
            
wb.save('output.xlsx')

Upvotes: 2

Related Questions