1

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

2
  • Some sample code would be useful. What module are you using? what os are you on? Commented Jul 12, 2009 at 19:46
  • I work with windows, excel 2003, some package called easyExcel - it is just a number of useful functions implemented with COM Commented Jul 12, 2009 at 20:05

6 Answers 6

3

As Roberto mentions, you can use xlwt and a trusty for-loop:

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')
Sign up to request clarification or add additional context in comments.

2 Comments

Of course it is possible to that, but my question was how to do it using AutoFill capability of Excel...
def _num_to_let(num): if num > 25: return _num_to_let(num/26-1) + chr(97+ num % 26) return chr(97+num)
0

If you are using COM bindings, then you can simply record a macro in Excel, then translate it into Python code.
If you are using xlwt, you have to resort to normal loops in python..

1 Comment

I am using COM, recorded macro, but I don't know how to translate the following: ActiveCell.FormulaR1C1 = "=R[-1]C[-7]/RC[-10]*R[-1]C" Range("M6").Select Selection.AutoFill Destination:=Range("M6:M592") Range("M6:M592").Select
0

Sasha,

Python code translated from your macro would look like this:

startCell = mySheet.Range("M6")
wholeRange = mySheet.Range("M6:M592")
startCell.FormulaR1C1 = "=R[-1]C[-7]/RC[-10]*R[-1]C"
startCell.AutoFill(Destination=wholeRange)

Haven't tested it, but I write this often at work. Let me know if it doesn't work.

2 Comments

@Greg: please edit your response so that the code shows up as 4 lines not 1 line ... hint: start each line with 4 spaces.
Hmm, I thought I did... OK, fixed it.
0

If you want to iterate in the horizontal direction, here is a function I use. 0 -> a, 26 -> aa, 723 -> aav

def _num_to_let(num):
        if num > 25:
            return _num_to_let(num/26-1) + chr(97+ num % 26)
        return chr(97+num)

Comments

0

If you want to iterate in xlwt for columns (in formulas) you can use Utils module from xlwt like this:

from xlwt import Utils
print Utils.rowcol_pair_to_cellrange(2,2,12,2)
print Utils.rowcol_to_cell(13,2)
>>>
C3:C13
C14

Comments

0

Using one of the COM wrapping libraries will allow you to use a User Defined Functions written in Python. This is the domain of xlwings and PyXll.

FlyingKoala also does this as it's an extension of xlwings. FlyingKoala has added ability to (on-the-fly) read Excel functions and convert them into Python code and evaluate the equation in Python.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.