I would like to insert a calculation in Excel using Python.
Generally it can be done by inserting a formula string into the relevant cell.
However, if i need to calculate a formula multiple times for the whole column
the formula must be updated for each individual cell. For example, if i need to
calculate the sum of two cells, then for cell C(k) the computation would be A(k)+B(k).
In excel it is possible to calculate C1=A1+B1 and then automatically expand the
calculation by dragging the mouse from C1 downwards.
My question is: Is it possible to the same thing with Python, i.e. to define a formula in only one cell and then to use Excel capabilities to extend the calculation for the whole column/row?
Thank you in advance,
Sasha
解决方案
As Roberto mentions, you can use
import xlwt
w = xlwt.Workbook()
ws = w.add_sheet('mysheet')
for i in range(10):
ws.write(i, 0, i)
ws.write(i, 1, i+1)
ws.write(i, 2, xlwt.Formula("$A$%d+$B$%d" % (i+1, i+1)))
w.save('myworkbook.xls')