Python Forum

Full Version: openpyxl insert list with formulas
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello
I have this code:
wb = Workbook()
ws = wb.active
listf = [[1,1,1,1], [2,2,2,2], [3,3,3,3]]
calc_lst = [['=СЧЁТЕСЛИ(listf[x];"<>")'], ["=СРЗНАЧ(listf[x]"], ["=СТАНДОТКЛОН(listf[x])"], ['=СТАНДОТКЛОН(listf[x])/корень(СЧЁТЕСЛИ(listf[x];"<>"))']]
def col_writer(row_number, column_number, lst):
    for col in range(len(lst)):
        for row in range(len(lst[col])):
            ws.cell(row+row_number, col+column_number, lst[col][row])
    return wb.save("sample.xlsx")
col_writer(1,2, listf)
and i need to insert: calc_lst in this file too under list: listf

Like this:
[/img]screenshot
Okey i rewrite my code and its work:
wb = Workbook()
ws = wb.active
listf = [[1,1,1,1], [2,2,2,2], [3,3,3,3]]


def col_writer(row_number, column_number, lst):
    for c_index, col in enumerate(lst, start=4):
        for r_index, row in enumerate(col, start=6):
            ws.cell(r_index, c_index, row)
        c = get_column_letter(c_index)  # column letter
        calc_lst = [f'=СРЗНАЧ({c}6:{c}35)', f'=СРЗНАЧ({c}6:{c}35)', f'=СТАНДОТКЛОН({c}6:{c}35)', f'=СТАНДОТКЛОН({c}6:{c}35)']
        for r_index, formula in enumerate(calc_lst, start=37):
            ws.cell(r_index, c_index, formula).number_format = numbers.FORMAT_GENERAL
    return wb.save('sample.xlsx')


col_writer(1,1,listf)
But my formulas work only if i open this xlsx file and double click to cell with formula, maybeanyone know how to fix it?