from openpyxl import load_workbook
from openpyxl.utils import get_column_letter
def read_excel_with_formulas(filename):
workbook = load_workbook(filename, data_only=True)
worksheet = workbook.active
for row in worksheet.iter_rows(values_only=True):
for cell in row:
if cell is not None and cell.startswith('='):
column_letter = get_column_letter(cell.column)
cell.value = worksheet[column_letter + str(cell.row)].value
workbook.save("updated_" + filename)
# Example usage
filein = "your_file.xlsx"
read_excel_with_formulas(filein)
1条答案
按热度按时间mkshixfv1#
如果我理解正确的话,你可以尝试使用openpyxl库读取Excel文件中包含公式的单元格的计算值:
load_workbook()
使用data_only=True
选项加载Excel文件,以加载计算值而不是公式。iter_rows()
和values_only=True
来迭代工作表中的每个单元格