Skip to content

Instantly share code, notes, and snippets.

@benninkcorien
Last active October 21, 2020 13:41
Show Gist options
  • Save benninkcorien/4e125d88706e4a69db9594c3c9483c41 to your computer and use it in GitHub Desktop.
Save benninkcorien/4e125d88706e4a69db9594c3c9483c41 to your computer and use it in GitHub Desktop.
openpyxl regex replace values in Excel column
import openpyxl
import re
wb = openpyxl.load_workbook('file01.xlsx')
# print(wb.sheetnames)
ws = wb["Sheet1"]
number_rows = ws.max_row
# Set Column number here !
for rownum in range(5, number_rows + 1):
try:
currentcell = ws.cell(row=rownum, column=8).value
print("Original", currentcell)
# replace anything that's not € or a digit . or , (OCR errors)
ws.cell(row=rownum, column=8).value = re.sub(r"[^€$\d\.\,]", "", currentcell)
print("Replaced with value: ", currentcell)
except:
print("Something went wrong")
wb.save(r'fixed01.xlsx')
@benninkcorien
Copy link
Author

This for some reason is not saving the regex replaced values to the new file..to be fixed.

@benninkcorien
Copy link
Author

Working now. (thanks to reddit, I missed a .value)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment