python可以用于excel计算吗,使用Python在Excel中计算公式

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')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值