Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.4 kB
6
Indexable
Never
def extend_formulas_in_excel(file_path, output_path):
    # Load the workbook and select the active worksheet
    workbook = openpyxl.load_workbook(file_path)
    print (workbook)
    sheet = workbook["Sheet1"]
    print (sheet)

    # Determine the maximum length of the data in the worksheet
    # for row in sheet.iter_rows(values_only=True):
    #     print(row)
    
    max_length = max(sheet.max_row, max(len(column) for column in sheet.columns))
    print (max_length)

    # Iterate through each column in the first row to check for formulas
    for col in sheet.iter_cols(min_row=2, max_row=2, values_only=False):
        first_cell = col[0]
        print (first_cell.value)
        if first_cell.value is not None and isinstance(first_cell.value, str) and first_cell.value.startswith('='):
            formula = first_cell.value
            print (formula)
            # Extend the formula down the column
            # for row in range(2, max_length + 1):
            #     cell = sheet.cell(row=row, column=first_cell.column)
                # Adjust the formula to the new row
                # trans = Translator(cell, origin=(1, 1))
                # new_formula = trans.translate(formula, row=row, col=first_cell.column)
                # cell.value = new_formula

    # Save the workbook (you might want to adjust the filename to avoid overwriting the original file)
    workbook.save(output_path)
Leave a Comment