Skip to content

Instantly share code, notes, and snippets.

@vekio
Last active October 24, 2020 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vekio/2cebd2db0f6d27692ede7dbf4e5579ae to your computer and use it in GitHub Desktop.
Save vekio/2cebd2db0f6d27692ede7dbf4e5579ae to your computer and use it in GitHub Desktop.
find and replace a value in a given column inside an excel
# conda env create -f excel-env.yml
name: excel
dependencies:
- python=3.7
- openpyxl
# 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