Oct-16-2019, 12:35 PM
Hello,
Newbie here too :)
For what I understand, at loop 1 you replace all the "X" by "value1" in the object wb in memory. At loop 2 no "X" to be replaced wil be found in wb.
Creating a new destination file object at each loop can allow you to save the X replaced without impacting the original file
Newbie here too :)
For what I understand, at loop 1 you replace all the "X" by "value1" in the object wb in memory. At loop 2 no "X" to be replaced wil be found in wb.
Creating a new destination file object at each loop can allow you to save the X replaced without impacting the original file
import openpyxl from openpyxl import load_workbook from openpyxl import Workbook #add wb = openpyxl.load_workbook('questions.xlsx') ws = wb.worksheets[0] ws2 = wb.worksheets[1] list = ["value1", "value2"] for account in list: wdest=Workbook() #add wdest.create_sheet('result') #add Sheetdest=wdest['result'] #add for i in range(1, 3): if ws.cell(column=1, row=i).value is not None: Sheetdest.cell(column=1, row=i).value = ws.cell(column=1, row=i).value.replace(str("X"), account) #modify for i in range(1, 20): if ws2.cell(column=2, row=i).value is not None: Sheetdest.cell(column=2, row=i).value = ws2.cell(column=2, row=i).value.replace(str("X"), account)#modify wdest.save('./test/' +'questions_' + account + '.xlsx') #modifyHave a nice day :)