Last active
October 24, 2020 17:56
-
-
Save vekio/2cebd2db0f6d27692ede7dbf4e5579ae to your computer and use it in GitHub Desktop.
find and replace a value in a given column inside an excel
This file contains 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
# conda env create -f excel-env.yml | |
name: excel | |
dependencies: | |
- python=3.7 | |
- openpyxl |
This file contains 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
# python excel.py example.xlsx B value "new value" | |
import sys | |
from openpyxl import load_workbook | |
def main(argv): | |
filename = argv[0] | |
col = argv[1] | |
value = argv[2] | |
replace = argv[3] | |
workbook = load_workbook(filename=filename) | |
sheet = workbook.active | |
print("Replacing at {} in the column {}".format(sheet.title, col)) | |
for cell in sheet[col]: | |
cell.value = str(cell.value).replace(value, replace) | |
filename="replaced-"+filename | |
workbook.save(filename=filename) | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment