Oct-16-2019, 10:49 AM
Hi i am new here and into coding with python ( thanks for patience :) ).
I am trying to write simple for loop which will use openpyxl to "search and replace" inside excel file and do this with the values in the list and save results as separate excel file for each list value.
Two separate files called questions_value1.xlsx and questions_value2.xlsx
But inside both excel files "X" is replaced by value1 only.
Result i am looking for:
Two separate excel files called questions_value1.xlsx and questions_value2.xlsx where:
"X" will be replaced by value1 in file questions_value1.xlsx and by value2 in questions_value2.xlsx.
Can someone please explain to me where is the "catch"? I did a lot of reading and googling back and forth but end up only with broken code and messed up head :)
Thanks.
I am trying to write simple for loop which will use openpyxl to "search and replace" inside excel file and do this with the values in the list and save results as separate excel file for each list value.
import openpyxl from openpyxl import load_workbook wb = openpyxl.load_workbook('questions.xlsx') ws = wb.worksheets[0] ws2 = wb.worksheets[1] list = ["value1", "value2"] for account in list: for i in range(1, 3): if ws.cell(column=1, row=i).value is not None: ws.cell(column=1, row=i).value = ws.cell( column=1, row=i).value.replace(str("X"), account) for i in range(1, 125): if ws2.cell(column=2, row=i).value is not None: ws2.cell(column=2, row=i).value = ws2.cell( column=2, row=i).value.replace(str("X"), account) wb.save('./test/' + 'questions_' + account + '.xlsx')My result now is:
Two separate files called questions_value1.xlsx and questions_value2.xlsx
But inside both excel files "X" is replaced by value1 only.
Result i am looking for:
Two separate excel files called questions_value1.xlsx and questions_value2.xlsx where:
"X" will be replaced by value1 in file questions_value1.xlsx and by value2 in questions_value2.xlsx.
Can someone please explain to me where is the "catch"? I did a lot of reading and googling back and forth but end up only with broken code and messed up head :)
Thanks.