我试图将公式写入单元格并写入整个列。但是,对于列中的每个单元格,它会显示=(E2-F2)/C2
。对于同一列中的每个单元格,如何获取它,以便读取(E3-F3)/C3
等等?
import openpyxl
wb = openpyxl.load_workbook('yes.xlsx')
Sheet = wb.get_sheet_by_name('Sheet1')
n = '=(E2-F2)/C2'
for cellObj in list(Sheet.columns)[6]:
cellObj.value = n
wb.save("row_creation_loop.xlsx")
答案 0 :(得分:2)
从我的评论中,这里是完整的for
- 循环代码:
# n = '=(E2-F2)/C2' # move this assignment into the for-loop
for row, cellObj in enumerate(list(Sheet.columns)[6]):
print cellObj
n = '=(E%d-F%d)/C%d' % (row, row, row)
# print n # check that n gets assigned correct string value
cellObj.value = n
希望这有帮助。
答案 1 :(得分:0)
您必须在r(row, row, row)
上添加+1。
# n = '=(E2-F2)/C2' # move this assignment into the for-loop
for row, cellObj in enumerate(list(Sheet.columns)[6]):
print cellObj
n = '=(E%d-F%d)/C%d' % (row, row, row)
# print n # check that n gets assigned correct string