Oct-31-2022, 03:55 PM
I think this might be what you want.
import openpyxl def set_cell_text(cell, text): cell.value = text cell.data_type = "s" cell.quotePrefix = True wb = openpyxl.load_workbook("data.xlsx") set_cell_text(wb.active["B3"], "=A1+A2") wb.active["B4"] = "=A1+A2" wb.save("data2.xlsx")For testing I made a small spreadsheet named "data.xlsx".
Output:A1: 3, B1: =A1+A2
A2: 4, B2: '=A1+A2
Running the program creates a new spreadsheet named "data2.xlsx" with new cells A3 which acts just like A2 (text) and A4 which acts just like A1 (formula), all the way down to showing the leading quote for text cells.