openpyxl 遍历行并应用公式
openpyxl iterate through rows and apply formula
我正在尝试遍历 Excel 工作表中特定列的行,应用公式并保存输出。我正在努力使我的代码正确,并且不确定下一步该去哪里。
到目前为止我的代码:
import openpyxl
wb = openpyxl.load_workbook('test-in.xlsx')
sheet = wb.worksheets[2]
maxRow = sheet.max_row
for row in range(2, maxRow)
wb.save('test-out.xlsx')
所以我不清楚如何编写 for 循环以在 E 列中写入应用 =CLEAN(D2) 公式的结果。我可以将公式应用于单个单元格:
sheet['I2'] = '=CLEAN(D2)'
但是我不确定如何将其合并到我的 for 循环中!
非常感谢任何帮助...
试试这个(max_row_num 是你的 maxRow - 在 Python 中我们通常不对变量使用 camelCase):
for row_num in range(2, max_row_num):
sheet['E{}'.format(row_num)] = '=CLEAN(D{})'.format(row_num)
文档中对此进行了介绍:http://openpyxl.readthedocs.io/en/latest/tutorial.html#accessing-many-cells
我正在尝试遍历 Excel 工作表中特定列的行,应用公式并保存输出。我正在努力使我的代码正确,并且不确定下一步该去哪里。
到目前为止我的代码:
import openpyxl
wb = openpyxl.load_workbook('test-in.xlsx')
sheet = wb.worksheets[2]
maxRow = sheet.max_row
for row in range(2, maxRow)
wb.save('test-out.xlsx')
所以我不清楚如何编写 for 循环以在 E 列中写入应用 =CLEAN(D2) 公式的结果。我可以将公式应用于单个单元格:
sheet['I2'] = '=CLEAN(D2)'
但是我不确定如何将其合并到我的 for 循环中!
非常感谢任何帮助...
试试这个(max_row_num 是你的 maxRow - 在 Python 中我们通常不对变量使用 camelCase):
for row_num in range(2, max_row_num):
sheet['E{}'.format(row_num)] = '=CLEAN(D{})'.format(row_num)
文档中对此进行了介绍:http://openpyxl.readthedocs.io/en/latest/tutorial.html#accessing-many-cells