1

I am currently trying to average 3 excel columns(Col C to E) into and new 4th column (Col F). When I look up how to do this with openpyxl the code looks like this:

from openpyxl import load_workbook

wb = load_workbook('PythontestAvg.xlsx')
sheet1 = wb['full trace']
sheet2 = wb['full trace Copy']

sheet2["F1"] = '=AVERAGE(C1:E1)'
sheet2["F2"] = '=AVERAGE(C2:E2)'

However, doing this is very tedious for 1500 cells from F1 to F1501. Is there a way to write a code that will let me average each of the rows for the 3 columns and paste in Column F? Basically a code that will give the same result as:

sheet2["F1"] = '=AVERAGE(C1:E1)'
sheet2["F2"] = '=AVERAGE(C2:E2)'
sheet2["F3"] = '=AVERAGE(C3:E3)'
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
sheet2["F1501"] = '=AVERAGE(C1501:E1501)'

Please let me know if any more clarification is needed. Thanks in advance

1 Answers1

0

Seems like you are looking for a for loop similar to this:

for i in range(1, 1502):
    sheet2["F" + str(i)] = '=AVERAGE(C%s:E%s)' % (i,i)
Max Kaha
  • 902
  • 5
  • 12
  • yes exactly! thank you so much. Sorry for the basic question this is my first time coding and trying to use openpyxl. – Houman Qadir Nov 04 '19 at 22:44
  • @HoumanQadir No problem, if the question is solved feel free to accept my answer so others can quickly see it. – Max Kaha Nov 04 '19 at 22:50