I use openpyxl package to write in a range of cells and refresh the workbook. Before that, the input is split into max 30 list element in order to not overload Excel. The multi file will be combined later. The code work seamless but the issue here that when I check the created file, I found the number from the template file in the input sheet. It should be replaced by the company_ids list provided in the code and refresh based on that. Is there something wrong in the code?
xlapp = win32com.client.DispatchEx("Excel.Application")
xlapp.DisplayAlerts = False
xlapp.Visible = True
company_ids = ['IQ35312', 'IQ93486', 'IQ93552', 'IQ120623']
ids_chunks = [company_ids[x:x+30] for x in range(0, len(company_ids), 30)]
def changeCells(ids, output_file):
wb = load_workbook('path\\template.xlsx')
ws = wb.active
ws.active = 0
for row in ws['C20:C50']:
for cell in row:
cell.value = None
for row in zip(range(20,50), ids):
ws.cell(row[0], 3).value = row[1]
wb.save('path'+output_file)
wb.close()
files = []
for i in range(len(ids_chunks)):
changeCells(ids_chunks[i], f'Beta_chunk_nb{i}.xlsx')
files.append(f'Beta_chunk_nb{i}.xlsx')
wb = xlapp.Workbooks.Open('path\\'f'Beta_chunk_nb{i}.xlsx')
wb.RefreshAll()
xlapp.CalculateUntilAsyncQueriesDone()
wb.Save()
wb.Close()
EDIT:
I realized that the error is due to excel changing normal formula to array formula.
Eg.
=TRANSPOSE(FILTER(UNIQUE(Input!C20:C1048576),UNIQUE(Input!C20:C1048576)<>""))
has changed to
{=TRANSPOSE(FILTER(UNIQUE(Input!C20:C1048576),UNIQUE(Input!C20:C1048576)<>""))}
Why is that and how can I prevent this?