Last active
October 21, 2020 13:41
-
-
Save bioluminesceme/4e125d88706e4a69db9594c3c9483c41 to your computer and use it in GitHub Desktop.
openpyxl regex replace values in Excel column
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
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
This for some reason is not saving the regex replaced values to the new file..to be fixed.